Serialize process spawning across threads with a lock#728
Open
veeceey wants to merge 1 commit intoMagicStack:masterfrom
Open
Serialize process spawning across threads with a lock#728veeceey wants to merge 1 commit intoMagicStack:masterfrom
veeceey wants to merge 1 commit intoMagicStack:masterfrom
Conversation
Replace the RuntimeError-raising check for concurrent process spawning with a threading.Lock that serializes spawns across different event loops running in separate threads. The old approach would fail with "Racing with another loop to spawn a process" when multiple threads tried to spawn processes concurrently, since the global pthread_atfork handlers can only be active for one loop at a time. Now instead of failing, concurrent spawns wait for the lock, allowing them to proceed sequentially. The lock is properly released in all error paths via a finally block. Fixes MagicStack#508
Author
|
Couldn't build locally (shallow clone, no libuv submodule), but the change is straightforward — replacing the The key points:
Looking forward to CI results to confirm this works end-to-end. |
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.
When multiple threads each run their own event loop and try to spawn processes concurrently, they race on the global
pthread_atforkhandlers, which can only be active for one loop at a time. The current code detects this race and raisesRuntimeError("Racing with another loop to spawn a process"), which isn't great — it means users have to build their own retry logic or serialization.This replaces the error-raising approach with a
threading.Lockthat serializes process spawning across threads. Instead of failing, concurrent spawns just wait their turn. The lock is held only during the critical fork section (setting uppthread_atforkhandlers, callinguv_spawn, and cleaning up), so it doesn't block any longer than necessary.The lock is properly released in all error paths through a
finallyblock that also cleans up the__forkingstate if something goes wrong mid-fork.Repro from the issue now works without errors:
Fixes #508