Skip to content

fix: don't hang stem_words_parallel in a forked child - #43

Open
tomaionescu wants to merge 1 commit into
qdrant:masterfrom
tomaionescu:fix/fork-hang
Open

tomaionescu wants to merge 1 commit into
qdrant:masterfrom
tomaionescu:fix/fork-hang

Conversation

@tomaionescu

Copy link
Copy Markdown

stem_words_parallel blocks forever in a fork()ed child once the parent process has called it. This happens with multiprocessing using the fork start method (the default on Linux up to Python 3.13) after the parent has already stemmed something in parallel.

import multiprocessing as mp
from py_rust_stemmers import SnowballStemmer

WORDS = ["running", "jumps", "easily"] * 10_000

def child(q):
    q.put(SnowballStemmer("english").stem_words_parallel(WORDS)[:3])

if __name__ == "__main__":
    SnowballStemmer("english").stem_words_parallel(WORDS)  # starts rayon's global pool
    ctx = mp.get_context("fork")
    q = ctx.Queue()
    p = ctx.Process(target=child, args=(q,), daemon=True)
    p.start()
    p.join(timeout=10)
    print("hung" if p.is_alive() else q.get())

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 first stem_words_parallel queues 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. When stem_words_parallel runs in a different process, it falls back to stem_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):

  • New test test_stem_words_parallel_in_forked_child forks 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 where os.fork doesn't exist. Without the fix it fails with Failed: stem_words_parallel hung in a forked child; with the fix all 13 tests pass.
  • cargo fmt -- --check and cargo clippy -- -D warnings pass.
  • Not run on Linux; this PR's CI runs the tests there.

AI-assisted.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant