[Rd] Is it possible to gracefully interrupt a child R process on MS Windows?

2025-05-11 Thread Henrik Bengtsson
In help("pskill", package = "tools") is says:

  Only SIGINT and SIGTERM will be defined on Windows, and pskill will
always use the Windows system call TerminateProcess.

As far as I understand it, TerminateProcess [1] terminates the process
"quite abruptly". Specifically, it is not possible for the process to
intercept the termination and gracefully shutdown. In R terms, we
cannot rely on:

tryCatch({
  ...
}, interrupt = function(int) {
  ## cleanup
})

Similarly, it does not look like R itself can exit gracefully. For
example, when signalling pskill(pid, signal = SIGINT) to another R
process, that R process leaves behind its tempdir(). In contrast, if
the user interrupts the process interactively (Ctrl-C), there is an
'interrupt' condition that can be caught, and R cleans up after itself
before exiting.

QUESTION:

Is it possible to gracefully interrupt a child R process on MS
Windows, e.g. a PSOCK cluster node? (I don't think so, but I figure
it's worth asking)


SUGGESTIONS:

Also, if my understanding that TerminateProcess is abrupt is correct,
and there is no way to exit gracefully, would it make sense to clarify
this fact in help("pskill", package = "tools")? Right now you either
have to know how 'TerminateProcess' works, or run various tests on MS
Windows to figure out the current behavior.

Also, would a better signal mapping be:

  Only SIGKILL will be defined on Windows, and pskill will always use
the Windows system call TerminateProcess. Signals SIGINT and SIGTERM
are supported for backward compatible reasons, but are effectively
identical to SIGKILL.

? That would change the expectations on what will happen for people
coming from the POSIX world.

[1] 
https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-terminateprocess

/Henrik

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] Why does format.default skip dispatch for list items?

2025-05-11 Thread Michael Chirico
This is the documented behavior since r35262 (2005), from ?format

> If x is a list, the result is a character vector obtained by applying 
> format.default(x, ...) to each element of the list

One consequence is that we can't add "nice" printing methods for
list-of-object columns in data.frames:

https://github.com/eddelbuettel/rprotobuf/issues/109

library(RProtoBuf)
person_desc <- P("tutorial.Person")
DF <- data.frame(id = 1:2)
DF$person = list(person_desc$new(name = "Me"), person_desc$new(name =
"You", email = "y...@u.com"))

DF
#   id  person
# 1  1 
# 2  2 
 ^ from format.default, with option to make it more informative

One assumes that at this point, there are back-compatibility concerns
since this is the documented behavior.

Is there no other option than to make the 'person' column itself an S3
class with an associated format method?

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Is it possible to gracefully interrupt a child R process on MS Windows?

2025-05-11 Thread Ivan Krylov via R-devel
On Sun, 11 May 2025 10:58:18 -0700
Henrik Bengtsson  wrote:

> Is it possible to gracefully interrupt a child R process on MS
> Windows, e.g. a PSOCK cluster node?

Not in the general case (I think, based on the code paths leading to
Rf_onintr() on Windows), but PSOCK cluster nodes are instances of
Rscript.exe running the terminal front-end, and the terminal front-end
interrupts R upon receipt of Ctrl+C and Ctrl+Break console events:
https://learn.microsoft.com/en-us/windows/console/setconsolectrlhandler

This makes it possible for ps::ps_interrupt() to spawn a child process
to attach to the console where Rscript.exe is running and generate this
event:
https://github.com/r-lib/ps/blob/042d4836ac584c95a59985171fdfa3b6baf2fa6c/src/interrupt.c#L33-L35

This probably needs specially written code in the children that expects
to be interrupted in order to work reliably, but interrupting the
children and then interrupting the parent and submitting another job to
the PSOCK cluster seems to have worked for me on R-4.5.0.

-- 
Best regards,
Ivan

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel