r-lib / later

Schedule an R function or formula to run after a specified period of time.
https://r-lib.github.io/later
Other
137 stars 27 forks source link

Callback registries should be cleared when process is forked #142

Open wch opened 3 years ago

wch commented 3 years ago

This is a follow-up to #141.

When the R process is forked with future, the child process gets a copy of the event loop, but it should be cleared out in the child process.

Example:

library(later)
library(future)

# Enable forked process
options(future.fork.enable = TRUE)
plan(multicore, workers = 2)

ff <- function(){
  message( Sys.getpid())
  later::later(ff, 2)
}
ff()

fs <- replicate(2, {
  future({
    for (i in 1:1000) {
      later::run_now(timeout = 1)
    }
  })
})

This will output:

18235
18241
18242
18235
18241
18242
...

The callback registries should be cleared out when the process is forked. However, we have to be careful about how we do this. What happens if the fork happens in the middle of executing a callback? Is it safe to clear out the C++ data structures at any time that a fork could happen? (Normally the callbacks are removed after being executed.)