-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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
gh-102512: Fix threading after os.fork() called from a foreign thread #113261
Conversation
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
Thank you for your review Victor. I added more output in tests and added a test for foreign thread without DummyThread. The code was significantly rewritten. Replacing a DummyThread with a MainThread leaves a DummyThread thread with the same ident (this is disturbing) in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
I just suggest minor change to make the test easier to read, feel free to ignore.
I dislike setting self.__class__
, but well, it works as expected and the new tests are exhaustive, so I'm fine with it.
Misc/NEWS.d/next/Library/2023-03-08-00-02-30.gh-issue-102512.LiugDr.rst
Outdated
Show resolved
Hide resolved
Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.11. |
Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12. |
Sorry, @serhiy-storchaka, I could not cleanly backport this to
|
…alled from a foreign thread (pythonGH-113261) Always set a _MainThread as a main thread after os.fork() is called from a thread started not by the threading module. A new _MainThread was already set as a new main thread after fork if threading.current_thread() was not called for a foreign thread before fork. Now, if it was called before fork, the implicitly created _DummyThread will be turned into _MainThread after fork. It fixes, in particularly, an incompatibility of _DummyThread with the threading shutdown logic which relies on the main thread having tstate_lock. (cherry picked from commit 49785b0) Co-authored-by: Serhiy Storchaka <[email protected]> Co-authored-by: Marek Marczykowski-Górecki <[email protected]>
GH-114430 is a backport of this pull request to the 3.12 branch. |
…ork() called from a foreign thread (pythonGH-113261) Always set a _MainThread as a main thread after os.fork() is called from a thread started not by the threading module. A new _MainThread was already set as a new main thread after fork if threading.current_thread() was not called for a foreign thread before fork. Now, if it was called before fork, the implicitly created _DummyThread will be turned into _MainThread after fork. It fixes, in particularly, an incompatibility of _DummyThread with the threading shutdown logic which relies on the main thread having tstate_lock. (cherry picked from commit 49785b0) Co-authored-by: Marek Marczykowski-Górecki <[email protected]>
…ork() called from a foreign thread (pythonGH-113261) Always set a _MainThread as a main thread after os.fork() is called from a thread started not by the threading module. A new _MainThread was already set as a new main thread after fork if threading.current_thread() was not called for a foreign thread before fork. Now, if it was called before fork, the implicitly created _DummyThread will be turned into _MainThread after fork. It fixes, in particularly, an incompatibility of _DummyThread with the threading shutdown logic which relies on the main thread having tstate_lock. (cherry picked from commit 49785b0) Co-authored-by: Serhiy Storchaka <[email protected]> Co-authored-by: Marek Marczykowski-Górecki <[email protected]>
GH-114431 is a backport of this pull request to the 3.11 branch. |
…called from a foreign thread (GH-113261) (GH-114431) Always set a _MainThread as a main thread after os.fork() is called from a thread started not by the threading module. A new _MainThread was already set as a new main thread after fork if threading.current_thread() was not called for a foreign thread before fork. Now, if it was called before fork, the implicitly created _DummyThread will be turned into _MainThread after fork. It fixes, in particularly, an incompatibility of _DummyThread with the threading shutdown logic which relies on the main thread having tstate_lock. (cherry picked from commit 49785b0) Co-authored-by: Marek Marczykowski-Górecki <[email protected]>
…alled from a foreign thread (pythonGH-113261) Always set a _MainThread as a main thread after os.fork() is called from a thread started not by the threading module. A new _MainThread was already set as a new main thread after fork if threading.current_thread() was not called for a foreign thread before fork. Now, if it was called before fork, the implicitly created _DummyThread will be turned into _MainThread after fork. It fixes, in particularly, an incompatibility of _DummyThread with the threading shutdown logic which relies on the main thread having tstate_lock. (cherry picked from commit 49785b0) Co-authored-by: Serhiy Storchaka <[email protected]> Co-authored-by: Marek Marczykowski-Górecki <[email protected]>
…called from a foreign thread (GH-113261) (GH-114430) Always set a _MainThread as a main thread after os.fork() is called from a thread started not by the threading module. A new _MainThread was already set as a new main thread after fork if threading.current_thread() was not called for a foreign thread before fork. Now, if it was called before fork, the implicitly created _DummyThread will be turned into _MainThread after fork. It fixes, in particularly, an incompatibility of _DummyThread with the threading shutdown logic which relies on the main thread having tstate_lock. (cherry picked from commit 49785b0) Co-authored-by: Serhiy Storchaka <[email protected]> Co-authored-by: Marek Marczykowski-Górecki <[email protected]>
…alled from a foreign thread (pythonGH-113261) Always set a _MainThread as a main thread after os.fork() is called from a thread started not by the threading module. A new _MainThread was already set as a new main thread after fork if threading.current_thread() was not called for a foreign thread before fork. Now, if it was called before fork, the implicitly created _DummyThread will be turned into _MainThread after fork. It fixes, in particularly, an incompatibility of _DummyThread with the threading shutdown logic which relies on the main thread having tstate_lock. Co-authored-by: Marek Marczykowski-Górecki <[email protected]>
…alled from a foreign thread (pythonGH-113261) Always set a _MainThread as a main thread after os.fork() is called from a thread started not by the threading module. A new _MainThread was already set as a new main thread after fork if threading.current_thread() was not called for a foreign thread before fork. Now, if it was called before fork, the implicitly created _DummyThread will be turned into _MainThread after fork. It fixes, in particularly, an incompatibility of _DummyThread with the threading shutdown logic which relies on the main thread having tstate_lock. Co-authored-by: Marek Marczykowski-Górecki <[email protected]>
Based on #102517. It recreates a
_DummyThread
as_MainThread
after fork.os.fork()
called from DummyThread confuses threading shutdown logic #102512