Skip to content

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

Merged
merged 3 commits into from
Feb 25, 2023

Conversation

kumaraditya303
Copy link
Contributor

@kumaraditya303 kumaraditya303 commented Feb 24, 2023

@kumaraditya303 kumaraditya303 added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error needs backport to 3.9 only security fixes needs backport to 3.10 only security fixes needs backport to 3.11 only security fixes 🔨 test-with-buildbots Test PR w/ buildbots; report in status section labels Feb 24, 2023
@bedevere-bot
Copy link

🤖 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.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Feb 24, 2023
Copy link
Member

@ericsnowcurrently ericsnowcurrently left a 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.

@kumaraditya303
Copy link
Contributor Author

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.

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.

@kumaraditya303
Copy link
Contributor Author

The buildbots failures are unrelated so merging. https://buildbot.python.org/all/#/builders/259/builds/681/steps/5/logs/stdio

@kumaraditya303 kumaraditya303 merged commit 5f11478 into python:main Feb 25, 2023
@miss-islington
Copy link
Contributor

Thanks @kumaraditya303 for the PR 🌮🎉.. I'm working now to backport this PR to: 3.9, 3.10, 3.11.
🐍🍒⛏🤖

@kumaraditya303 kumaraditya303 deleted the deadlock branch February 25, 2023 06:51
@miss-islington
Copy link
Contributor

Sorry, @kumaraditya303, I could not cleanly backport this to 3.11 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker 5f11478ce7fda826d399530af4c5ca96c592f144 3.11

@miss-islington
Copy link
Contributor

Sorry @kumaraditya303, I had trouble checking out the 3.10 backport branch.
Please retry by removing and re-adding the "needs backport to 3.10" label.
Alternatively, you can backport using cherry_picker on the command line.
cherry_picker 5f11478ce7fda826d399530af4c5ca96c592f144 3.10

@miss-islington
Copy link
Contributor

Sorry, @kumaraditya303, I could not cleanly backport this to 3.9 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker 5f11478ce7fda826d399530af4c5ca96c592f144 3.9

@bedevere-bot
Copy link

GH-102234 is a backport of this pull request to the 3.11 branch.

@bedevere-bot
Copy link

GH-102235 is a backport of this pull request to the 3.10 branch.

@bedevere-bot
Copy link

GH-102236 is a backport of this pull request to the 3.9 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.9 only security fixes label Feb 25, 2023
kumaraditya303 added a commit to kumaraditya303/cpython that referenced this pull request Feb 25, 2023
…states (pythonGH-102222).

(cherry picked from commit 5f11478)

Co-authored-by: Kumar Aditya <[email protected]>
kumaraditya303 added a commit to kumaraditya303/cpython that referenced this pull request Feb 25, 2023
… states (pythonGH-102222).

(cherry picked from commit 5f11478)

Co-authored-by: Kumar Aditya <[email protected]>
kumaraditya303 added a commit to kumaraditya303/cpython that referenced this pull request Feb 25, 2023
… states (pythonGH-102222).

(cherry picked from commit 5f11478)

Co-authored-by: Kumar Aditya <[email protected]>
@kumaraditya303
Copy link
Contributor Author

Regardless I'll address that separately in a new issue

#102233

kumaraditya303 added a commit that referenced this pull request Feb 25, 2023
#102234)

[3.11] GH-102126: fix deadlock at shutdown when clearing thread states (GH-102222)

(cherry picked from commit 5f11478)
kumaraditya303 added a commit that referenced this pull request Mar 3, 2023
#102235)

[3.10] GH-102126: fix deadlock at shutdown when clearing thread states (GH-102222).
(cherry picked from commit 5f11478)
@bedevere-bot
Copy link

GH-102236 is a backport of this pull request to the 3.9 branch.

ambv pushed a commit that referenced this pull request Mar 28, 2023
gentoo-bot pushed a commit to gentoo/cpython that referenced this pull request May 21, 2024
… state… (python#102235)

[3.10] pythonGH-102126: fix deadlock at shutdown when clearing thread states (pythonGH-102222).
(cherry picked from commit 5f11478)
gentoo-bot pushed a commit to gentoo/cpython that referenced this pull request Sep 19, 2024
… state… (python#102235)

[3.10] pythonGH-102126: fix deadlock at shutdown when clearing thread states (pythonGH-102222).
(cherry picked from commit 5f11478)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants