Skip to content
Prev 10756 / 10988 Next

[Rcpp-devel] RCPP_USE_UNWIND_PROTECT by default

Thanks all. My summary here differs a bit though:

1. The point that a longjump does not necessarily represent an error
is crucial here. That's the rationale behind Lionel's implementation
of this exception. My understanding then is that client packages
should *never ever* catch it, and thus it shouldn't inherit from
std::exception.

2. UnwindProtect is a mechanism introduced in R back in 2017 for fast
and safe C++ stack unwinding, so I think it's time we stick to it.
When enabled, Rcpp leverages this via Rcpp_fast_eval. Before that,
Rcpp_eval was/is... kind of hackish (no offense intended; it was the
best thing to do before R properly supported C++ via UnwindProtect),
because it relied on tryCatch, which sometimes resulted in several
tryCatch stacked together. Rcpp_fast_eval is called in a number of
places in Rcpp, and thus client packages would greatly benefit from
finally switching to UnwindProtect, including V8.

3. But then, V8 *really needs* to catch R errors in the JS virtual
machine. So the solution... is to add *a* tryCatch in the proper place
(not a matrioska of tryCatches, as it happens without UnwindProtect).
And this is exactly what Jeroen did [1]. I think that the solution is
brilliant and the proper way to go here (as it happens, that tryCatch
was in place before, for other reasons I suppose, and now it has been
repurposed to properly catch errors too).

4. Another way to address this was mentioned by Lionel: Rcpp_eval is
still there. Arguably though, this interface is not very
user-friendly, and Rcpp::Function is much nicer. I don't think that
Rcpp::Function should switch to Rcpp_eval, but we could offer some
kind of Rcpp::TryCatchFunction (ok, the name can be improved) that
uses Rcpp_eval, or some kind of wrapper class that forces
Rcpp::Function to use Rcpp_eval instead.

[1] https://github.com/jeroen/V8/commit/3258ce61dd02eaed51edef5391dc5afcd456d16c

Iñaki
On Mon, 7 Feb 2022 at 03:11, Kevin Ushey <kevinushey at gmail.com> wrote: