Repository navigation
fix: don't hang stem_words_parallel in a forked child - #43
Open
tomaionescu wants to merge 1 commit into
Open
tomaionescu wants to merge 1 commit into
tomaionescu wants to merge 1 commit into
Conversation
fork() copies rayon's global thread pool state into the child but not its worker threads. Once the parent had called stem_words_parallel, the same call in a forked child queued work that no thread would run and blocked forever. Remember the PID that started the pool and fall back to stem_words in any other process. A child whose parent never used the pool still gets its own.
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.
stem_words_parallelblocks forever in afork()ed child once the parent process has called it. This happens withmultiprocessingusing the fork start method (the default on Linux up to Python 3.13) after the parent has already stemmed something in parallel.With the 0.1.8 wheel from PyPI this prints
hung. Without the call in the parent, the child works.Cause:
into_par_iter()runs on rayon's global thread pool.fork()copies the pool's state into the child but only the calling thread, so the child's firststem_words_parallelqueues a job that no worker will ever run and waits on it forever. Rayon's global pool can't be rebuilt once it exists.Fix: remember the PID of the process that first used the pool in a static
AtomicU32. Whenstem_words_parallelruns in a different process, it falls back tostem_words. A forked child inherits the parent's value, so it takes this path. Results are identical; the child just stems sequentially. A child whose parent never used the pool still starts its own pool. Nothing changes on Windows, and there is no new dependency.If you would rather have a warning, or only a README note, instead of the silent fallback, I can change it.
Testing (macOS arm64, Python 3.14):
test_stem_words_parallel_in_forked_childforks after a parallel call and polls the child for up to 10 s, killing it on timeout so it can't hang CI. It is skipped whereos.forkdoesn't exist. Without the fix it fails withFailed: stem_words_parallel hung in a forked child; with the fix all 13 tests pass.cargo fmt -- --checkandcargo clippy -- -D warningspass.AI-assisted.