Skip to content

gh-102512: Fix threading after os.fork() called from a foreign thread #102517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from

Conversation

marmarek
Copy link
Contributor

@marmarek marmarek commented Mar 7, 2023

threading._shutdown() relies on _main_thread having _tstate_lock not None (there is assert for that). When fork is called from a foreign thread (aka DummyThread), it gets promoted to main thread, but remains very simplistic DummyThread. Especially, nobody initializes its _tstate_lock. threading._after_fork() handles the case of current thread not being in _active dict at all (by creating new MainThread object), but it doesn't handle the case of having DummyThread there already.

Fix this by initializing _tstate_lock in a DummyThread too.
See linked issue for more details, including reproducer.

The PR includes also regression test

marmarek added 3 commits March 8, 2023 00:13
If os.fork() is called from a DummyThread (a thread started not by the
threading module, in which somebody called threading.current_thread()),
it becomes main thread. Threading shutdown logic relies on the main
thread having tstate_lock, so initialize it for the DummyThread too.

Fixes python#102512
@serhiy-storchaka
Copy link
Member

More radical change #113261 was applied instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants