evaluate icon indicating copy to clipboard operation
evaluate copied to clipboard

C stack error are not caught

Open cderv opened this issue 4 years ago • 4 comments

I don't know if this is possible or not but currently we have this

> evaluate::evaluate("stop('a')")
[[1]]
$src
[1] "stop('a')"

attr(,"class")
[1] "source"

[[2]]
<simpleError in eval(expr, envir, enclos): a>

> evaluate::evaluate('e <- new.env()
+                    attr(e, "e") <- e
+                    object.size(e)')
Error: C stack usage  15922884 is too close to the limit

This was reported in https://github.com/tidyverse/reprex/issues/388

cderv avatar Nov 02 '21 13:11 cderv

try() works in these two cases:

try(object.size(e), silent = T)
try(withCallingHandlers(object.size(e)), silent = T)

but not in this one when an error handler is present:

try(withCallingHandlers(object.size(e), error = function(e) NULL), silent = T)

I don't know why yet.

yihui avatar Nov 02 '21 13:11 yihui

Saw this today: https://twitter.com/R_dev_news/status/1457626456301846533

Stack overflow errors are now signaled as errors inheriting from class ‘stackOverflowError’. See ‘?stackOverflowError’ for more details.

jennybc avatar Nov 08 '21 15:11 jennybc

Great! It seems we can just wait for the next release of R. Thanks for the info!

yihui avatar Nov 08 '21 16:11 yihui

While stackoverflow errors are now caught:

e <- new.env()
attr(e, "e") <- e
tryCatch(object.size(e), error = function(err) err)
#> <CStackOverflowError: C stack usage  7953636 is too close to the limit>

Created on 2024-01-11 with reprex v2.0.2.9000

And evaluate now "works", it still doesn't actually capture the error:

evaluate::evaluate('
  e <- new.env()
  attr(e, "e") <- e
  object.size(e)'
)
#> [[1]]
#> $src
#> [1] "\n"
#> 
#> attr(,"class")
#> [1] "source"
#> 
#> [[2]]
#> $src
#> [1] "  e <- new.env()\n"
#> 
#> attr(,"class")
#> [1] "source"
#> 
#> [[3]]
#> $src
#> [1] "  attr(e, \"e\") <- e\n"
#> 
#> attr(,"class")
#> [1] "source"
#> 
#> [[4]]
#> $src
#> [1] "  object.size(e)"
#> 
#> attr(,"class")
#> [1] "source"

Created on 2024-01-11 with reprex v2.0.2.9000

This is likely due to the special nature of stackoverflow errors (because the stack is "full", it's hard to run any additional code wrapped around the error, so will likely require some careful extra handling).

hadley avatar Jan 11 '24 13:01 hadley

I'm pretty sure it's not possible to capture this error in evaluate itself

hadley avatar Jun 14 '24 19:06 hadley