-
-
Notifications
You must be signed in to change notification settings - Fork 32k
GH-102126: fix deadlock at shutdown when clearing thread states #102222
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
Conversation
kumaraditya303
commented
Feb 24, 2023
•
edited by bedevere-bot
Loading
edited by bedevere-bot
- Issue: Possible deadlock at shutdown while recursively acquiring head lock #102126
dd672a9
to
1e6a87b
Compare
🤖 New build scheduled with the buildbot fleet by @kumaraditya303 for commit 1e6a87b 🤖 If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again. |
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
There's one case that could be problematic but (ISTM) is highly unlikely: some other thread might call PyThreadState_Delete*()
on one of the existing thread states, which would modify the linked list while we're walking it here. We could mitigate that (very small) risk by checking in those functions if the interpreter is finalizing and returning immediately (or blocking) if it is. FWIW, the status quo is already deficient in that case, and this PR only slightly adds to the problem. Regardless, the risk is fairly minimal so we can address that separately.
(Before merging, would you mind just double-checking that the risk actually is minimal? 😄)
One observation: the lock isn't strictly necessary here. It's needed for one of the following:
- in case a new thread state gets created for the interpreter
- in the case described above (
PyThreadState_Delete*()
was called in another thread)
At this point in interpreter finalization, neither of those things should be happening. So it may make sense to disallow them (or ignore/block, in the case of the second one). Alternately we could give each interpreter it's own lock (e.g. PyInterpreterState.threads.mutex
) to use when managing its thread states, as opposed to reusing the global "HEAD" lock like we currently do.
Yeah, I too noticed that but I am not concerned about that as at runtime shutdown such code is already unsafe and can crash or result in hang etc. Regardless I'll address that separately in a new issue and get this fixed first. Thanks for the review. |
The buildbots failures are unrelated so merging. https://buildbot.python.org/all/#/builders/259/builds/681/steps/5/logs/stdio |
Thanks @kumaraditya303 for the PR 🌮🎉.. I'm working now to backport this PR to: 3.9, 3.10, 3.11. |
Sorry, @kumaraditya303, I could not cleanly backport this to |
Sorry @kumaraditya303, I had trouble checking out the |
Sorry, @kumaraditya303, I could not cleanly backport this to |
GH-102234 is a backport of this pull request to the 3.11 branch. |
GH-102235 is a backport of this pull request to the 3.10 branch. |
GH-102236 is a backport of this pull request to the 3.9 branch. |
…states (pythonGH-102222). (cherry picked from commit 5f11478) Co-authored-by: Kumar Aditya <[email protected]>
… states (pythonGH-102222). (cherry picked from commit 5f11478) Co-authored-by: Kumar Aditya <[email protected]>
… states (pythonGH-102222). (cherry picked from commit 5f11478) Co-authored-by: Kumar Aditya <[email protected]>
|
GH-102236 is a backport of this pull request to the 3.9 branch. |
…GH-102222) (#102236) (cherry picked from commit 5f11478)
… state… (python#102235) [3.10] pythonGH-102126: fix deadlock at shutdown when clearing thread states (pythonGH-102222). (cherry picked from commit 5f11478)
… state… (python#102235) [3.10] pythonGH-102126: fix deadlock at shutdown when clearing thread states (pythonGH-102222). (cherry picked from commit 5f11478)