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

2025-04-27 Thread Duncan Murdoch
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

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


[Rd] sqrt(.Machine$double.xmax)^2 == Inf, but only on Windows in R

2025-04-27 Thread Pavel Krivitsky via R-devel
Hello,

Under R 4.5.0 on Windows (x86-64), I get:

> sqrt(.Machine$double.xmax)^2
[1] Inf
> sqrt(.Machine$double.xmax)*sqrt(.Machine$double.xmax)
[1] Inf

On other hand on other platforms, including Debian Linux (x86-64), I
get:

d> sqrt(.Machine$double.xmax)^2
[1] 1.797693134862315508561e+308
d> sqrt(.Machine$double.xmax)*sqrt(.Machine$double.xmax)
[1] 1.797693134862315508561e+308

Windows is running inside a VirtualBox instance on the same host as
Linux. I don't have direct results from the MacOS platforms, but based
on the symptoms that had led me to investigate, the behaviour is as the
Linux.

Adding to the mystery, if I implement the same operation in C, e.g.,

library(inline)
sqrsqrt <- cfunction(sig = c(x = "numeric"), language = "C", "
  double sqrtx = sqrt(Rf_asReal(x));
  return Rf_ScalarReal(sqrtx*sqrtx);
")

R on Linux yields:

d> sqrsqrt(.Machine$double.xmax)
[1] 1.797693134862315508561e+308

i.e., the same number, whereas R on Windows yields:

d> sqrsqrt(.Machine$double.xmax)
[1] 1.797693134862315508804e+308

which is not Inf but not the same as Linux either.

Lastly, on both platforms,

d> sqrsqrt(.Machine$double.xmax) < .Machine$double.xmax
[1] TRUE

I am not sure if this is a bug, intended behaviour, or something else.

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