[Rcpp-devel] Rcpp::exception + threads = disaster

Joshua N Pritikin jpritikin at pobox.com
Sat Feb 8 23:07:05 CET 2020


On Sat, Feb 08, 2020 at 01:44:51PM -0800, Kevin Ushey wrote:
> On Sat, Feb 8, 2020 at 12:52 PM Joshua N Pritikin <jpritikin at pobox.com> wrote:
> > If I'm reading it correctly, rcpp_set_stack_trace(Shield<SEXP>(stack_trace()))
> > will work equally well in either location. R can't introspect about
> > the C++ stack; It must be saving the R call stack.
> 
> This is incorrect; Rcpp is trying to save the C++ call stack:
> 
> https://github.com/RcppCore/Rcpp/blob/f9425eef20f483001105733bc379b76e9f323aff/src/api.cpp#L283-L319

Whoa, that's pretty cool :-)

> In theory, we might be able to avoid the creation of an intermediate R
> object when the exception is constructed -- patches are welcome.

Yeah, you just need to delay the part where you copy it to R,

 List trace = List::create(_["file" ] = file,
                                      _["line" ] = line,
                                      _["stack"] = res);
 trace.attr("class") = "Rcpp_stack_trace";

I'll code something up.

> I'm not sure why this data is stored within the R session rather than e.g. 
> as part of the exception object itself, but generally these sorts of 
> decisions are made for good reasons (e.g. complications with cross-module 
> exceptions).

The details of the R side are irrelevant. You can store the stack trace in
C++ STL containers and move it to R after the throw.

> As an aside, you could just use tfm::format() (bundled by Rcpp and
> used by stop() and friends) to build your formatted strings and then
> throw a regular C++ exception.

I know, but I'm trying to smooth the road for future developers.
Maybe I invest too much time and effort into altruism?


More information about the Rcpp-devel mailing list