add isForkedChild() for tbb fork-safety checks#244
Open
kevinushey wants to merge 1 commit intomasterfrom
Open
Conversation
closes #243. tbb does not support being used after fork(). downstream packages can't reliably detect this themselves because rcppparallel — and therefore tbb — may have been loaded into the parent long before the downstream package gets a chance to record a baseline pid. capture getpid() in R_init_RcppParallel and expose RcppParallel::isForkedChild() (c++) plus isForkedChild() (r) so callers can fall back to a serial path when running inside a fork()'d child (e.g. under parallel::mclapply). windows is a no-op (always false) since it has no fork().
Contributor
Author
|
@traversc Let me know if this looks sufficient? |
Contributor
|
Looks good! You might consider a different function name since it conflicts with parallelly::isForkedChild and has slightly different meaning. |
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.
Summary
RcppParallel::isForkedChild()(C++) andisForkedChild()(R), returningTRUEwhen the current process is afork()of the process in which RcppParallel (and TBB) was originally loaded.getpid()once inR_init_RcppParallel; the C++ surface is a non-inline function insrc/init.cpp, declared ininst/include/RcppParallel/Fork.h(matches the existingtbbParallelForpattern, resolved at runtime viaRTLD_GLOBALon Linux/macOS, linker-imported on Windows).FALSE) since Windows has nofork().Closes #243.
Why this lives in RcppParallel
Downstream packages can't implement this themselves: RcppParallel may be loaded into the parent process long before the downstream package, so any baseline PID a downstream captures (e.g. in its own static initializer, or
.onLoad) can already be a post-fork PID — exactly the false negative reproduced in the issue'slibrary(RcppParallel); mclapply({ library(qs2); ... })case. The baseline has to be captured by RcppParallel itself.Suggested usage
Test plan
R CMD INSTALLsucceeds on macOSFALSE; both children inparallel::mclapply(1:2, ...)returnTRUERcpp::sourceCppcallingRcppParallel::isForkedChild()