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

Joshua N Pritikin jpritikin at pobox.com
Fri Feb 7 02:47:57 CET 2020


The Rcpp::exception constructor does,

  rcpp_set_stack_trace(Shield<SEXP>(stack_trace()))

This can corrupt R if called within an OpenMP block.

It would be nice if there were some warnings about this (mis)usage OR, 
better, if Rcpp::exception was thread-safe.

Here's the usage that should be A-OK:

std::string err;
bool more = true;
#pragma omp parallel num_threads(numThreads)
while (more) {
  int tid = omp_get_thread_num();
  // ... do stuff ...
  try {
    // ... do stuff ...
    if (bad) stop("oops, this is bad");
  } catch (const std::exception& e) {
#pragma omp atomic
    err = e.what();
    more = false;
  } catch (...) {
    more = false;
  }
}
if (err.length()) stop("%s", err.c_str());

-- 
Joshua N. Pritikin, Ph.D.
Virginia Institute for Psychiatric and Behavioral Genetics
Virginia Commonwealth University
PO Box 980126
800 E Leigh St, Biotech One, Suite 1-133
Richmond, VA 23219
http://exuberant-island.surge.sh


More information about the Rcpp-devel mailing list