convert c++ exceptions to r errors in module .External entry points - #1496
Open
kevinushey wants to merge 1 commit into
Open
convert c++ exceptions to r errors in module .External entry points#1496kevinushey wants to merge 1 commit into
kevinushey wants to merge 1 commit into
Conversation
eddelbuettel
approved these changes
Aug 4, 2026
eddelbuettel
left a comment
Member
There was a problem hiding this comment.
LGTM.
Interesting that 'merely' wrapping the standard entry / exit of generated functions does the trick.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1495.
CppMethod__invoke,CppMethod__invoke_void,CppMethod__invoke_notvoid, andclass__newInstanceinsrc/module.cppare.Externalentry points that can throw C++ exceptions -- most notablyCHECK_DUMMY_OBJ'sRcpp::not_initialized, the designed error for using a module object created via the dummy-object path (new(Class)with no arguments, for a class without a default constructor). With noBEGIN_RCPP/END_RCPPin these functions, the exception escapes into R's C evaluator uncaught andstd::terminate()aborts the whole R session, instead of raising the intended R error. See #1495 for details.Changes:
src/module.cpp: wrap the four entry points inBEGIN_RCPP/END_RCPP, so exceptions are converted to R errors as elsewhere in Rcpp. On thenot_initializedpath this also unwinds the localXPtrobjects properly before the R error is raised.inst/tinytest/test_module.R: regression test -- calling a method on an uninitializedModuleRandomizermust raise the "C++ object not initialized" R error. Without the fix this test aborts the tinytest process.One pre-existing quirk surfaced while testing (left unchanged, noting it here): when a dummy object is garbage collected, the reference class finalizer calls
class_<T>::run_finalizer(), whoseXP(object)rejects the dummy pointer (an environment), printingError: Expecting an external pointer: [type=environment].at shutdown. That happens on current master for any dummy object whose finalizer runs and is independent of this change; it could be addressed separately by havingrun_finalizer()(orCppObject__finalize) skip the dummy instance.Verification:
SIGABRT, exit code 134) against current master and printscaught R error: C++ object not initialized. (Missing default constructor?)with exit code 0 against a build of this branch.R CMD checkwithRunAllRcppTests=yespasses on R 4.6.1.Checklist
R CMD checkstill passes all tests (checked locally withRunAllRcppTests=yeson R 4.6.1;--no-manual --no-vignettes, so the only flagged items were the two vignette-packaging warnings from the local--no-build-vignettesbuild)