Duncan Murdoch
2025-Apr-27 20:19 UTC
[Rd] on.exit() handler being interrupted by time limit
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
Tomas Kalibera
2025-May-12 14:41 UTC
[Rd] on.exit() handler being interrupted by time limit
On 4/27/25 22:19, Duncan Murdoch wrote:> > 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.Regarding base R, this works for me in Linux: > setTimeLimit(elapsed=10) > for(i in 1:1000) Sys.sleep(0.1) Error in Sys.sleep(0.1) : reached elapsed time limit That is,? the loop gets interrupted after about 10s. Reading the code of R, there is a check in Sys.sleep(), but only after the first wait (in a loop, iteratively waiting for the required duration). When polled events are not active, the first wait is the whole duration. I would read the documentation of ?setTimeLimit as that the check happens during Sys.sleep(), but not necessarily frequently, that "frequently" only refers to R code. And then the documentation would seem correct (or at least not contradicted). On the other hand, right, we could change Rsleep() to use a shorter sleep when elapsed time limit is in place, and that wouldn't contradict the documentation, either. Best Tomas> > Duncan Murdoch > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
Reasonably Related Threads
- make running on.exit expr uninterruptible
- BUG?: On Linux setTimeLimit() fails to propagate timeout error when it occurs (works on Windows)
- BUG?: On Linux setTimeLimit() fails to propagate timeout error when it occurs (works on Windows)
- BUG?: On Linux setTimeLimit() fails to propagate timeout error when it occurs (works on Windows)
- BUG?: On Linux setTimeLimit() fails to propagate timeout error when it occurs (works on Windows)