[Rd] on.exit() handler being interrupted by time limit

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Sun Apr 27 22:19:46 CEST 2025


This StackOverflow question:  https://stackoverflow.com/q/79594532 and 
this discussion on Github:  https://github.com/r-lib/evaluate/issues/242 
are talking about an interesting situation.

Under a fairly simple setup shown in both, setTimeLimit() can interrupt 
the on.exit() handler that was installed by evaluate::evaluate(), 
leaving the system in a weird state.

Here's the setup:

If the document

````
```{r}
ans <- 6 * 7
Sys.sleep(2)
```

Answer: `r ans`
````
is saved in timeout.Rmd and then the code below is run you get weird 
results, because plot hooks have been left in place:

```
setTimeLimit(elapsed = 1)       # set a timeout
knitr::knit("timeout.Rmd")      # knit the document which triggers it
print(1)                        # this works, but the hooks are still there
plot(1)                         # this triggers them
print(1)                        # Now this doesn't appear
```

What appears to be happening is that the timing error happens in the 
middle of an on.exit() handler execution.

I'm not sure if this is being caused by the complicated way that 
evaluate::evaluate() sets up the on.exit() handlers, or is being caused 
by a problem in R.

BTW, the help for setTimeLimit() says that the time limit check happens 
frequently during `Sys.sleep()`, but that doesn't appear to be true. 
I've tried a variation on the code above which sleeps for 20 seconds, 
even with a time limit of 1 second.

Duncan Murdoch



More information about the R-devel mailing list