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

Dirk Eddelbuettel edd at debian.org
Fri Feb 7 03:40:02 CET 2020


Joshua,

Thanks for reposting here.  A better place than my inbox, and I was rushed
earlier as I missed the line ...

On 6 February 2020 at 20:47, Joshua N Pritikin wrote:
| The Rcpp::exception constructor does,
| 
|   rcpp_set_stack_trace(Shield<SEXP>(stack_trace()))
| 
| This can corrupt R if called within an OpenMP block.

... here. In general, we can _never_ call back into R for anything,
exceptions or other things.

The RcppParallel package documentation is quite good and clear about this; it
even has extra data types RMatrix and RVector to stay away from R's memory
(which Rcpp is close to for performance and zero copy reasons). The Writing R
Extensions manual also as a little, but maybe less clearly.  In short, there
is simply "so much going with R" that it stands little chance of every being
threadsafe.

Which means that your OpenMP (or pthreads or TBB or ..) code has to stay away
from R.
 
| It would be nice if there were some warnings about this (mis)usage OR,
| better, if Rcpp::exception was thread-safe.

Maybe some clever folks will come up with a sanitizer variant; otherwise I
just don't see how.

Dirk


| 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
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
-- 
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rcpp-devel mailing list