r-lib/processx

process_interrupt() does not interrupt on Windows?

krlmlr opened this issue · 4 comments

POSIX

session <- callr::r_session$new()

session$supervise(TRUE)

session$call(function() {
  Sys.sleep(3)
})

system.time(print(session$poll_process(500)))
#> [1] "timeout"
#>    user  system elapsed 
#>   0.001   0.000   0.508
session$interrupt()
#> [1] TRUE
system.time(print(session$poll_process(500)))
#> [1] "ready"
#>    user  system elapsed 
#>   0.001   0.000   0.006

Created on 2021-12-04 by the reprex package (v2.0.1)

Windows

session <- callr::r_session$new()

session$supervise(TRUE)

session$call(function() {
  Sys.sleep(3)
})

system.time(print(session$poll_process(500)))
#> [1] "timeout"
#>    user  system elapsed
#>     0.0     0.0     0.5
session$interrupt()
#> [1] TRUE
system.time(print(session$poll_process(500)))
#> [1] "timeout"
#>    user  system elapsed
#>    0.00    0.00    0.49

Created on 2021-12-04 by the reprex package (v2.0.1)

Sending an interrupt on Windows is slower than on Unix, because it requires starting another process. This is slow on Windows, especially when you start the interrupting process the first time.

Another issue with this code is that the final elapsed time includes the time knitr uses to parse and run the various expressions. For a better test:

  • run this a couple of times, and
  • run it without reprex/knitr, or use a single code block, or a function,
  • or relax your time limits

E.g.

inttest <- function() {
  session <- callr::r_session$new()
  session$call(function() {
    Sys.sleep(3)
  })
  system.time(print(session$poll_process(500)))
  session$interrupt()
  system.time(print(session$poll_process(500)))
}
inttest()
#> [1] "timeout"
#> [1] "timeout"
#>    user  system elapsed
#>    0.00    0.00    0.48
inttest()
#> [1] "timeout"
#> [1] "ready"
#>    user  system elapsed
#>    0.00    0.00    0.21
inttest()
#> [1] "timeout"
#> [1] "ready"
#>    user  system elapsed
#>     0.0     0.0     0.2

Thanks, I will try increasing the timeout, this should give equivalent results?

Do you still have problems with this?

I am going to close this, but please reopen if you have issues.