Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-137433: Fix deadlock with stop-the-world and daemon threads#137735

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation

@colesbury
Copy link
Contributor

@colesburycolesbury commentedAug 13, 2025
edited by bedevere-appbot
Loading

There was a deadlock originally seen by Memray when a daemon thread enabled or disabled profiling while the interpreter was shutting down. I think this could also happen with garbage collection, but I haven't seen that in practice.

The daemon thread could be hung while trying acquire the global rwmutex that prevents overlapping global and per-interpreter stop-the-world events. Since it already held the main interpreter's stop-the-world lock, it also deadlocked the main thread, which is trying to perform interpreter finalization.

Swap the order of lock acquisition to prevent this deadlock. Additionally, refactor_PyParkingLot_Park so that the global buckets hashtable is left in a clean state if the thread is hung inPyEval_AcquireThread.

There was a deadlock originally seen by Memray when a daemon threadenabled or disabled profiling while the interpreter was shutting down.I think this could also happen with garbage collection, but I haven'tseen that in practice.The daemon thread could be hung while trying acquire the global rwmutexthat prevents overlapping global and per-interpreter stop-the-world events.Since it already held the main interpreter's stop-the-world lock, italso deadlocked the main thread, which is trying to perform interpreterfinalization.Swap the order of lock acquisition to prevent this deadlock.Additionally, refactor `_PyParkingLot_Park` so that the global bucketshashtable is left in a clean state if the thread is hung in`PyEval_AcquireThread`.
Copy link
Member

@ZeroIntensityZeroIntensity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

LGTM. I'm not a huge fan of usingPyEval_AcquireThread/PyEval_ReleaseThread over_PyThreadState_Attach/_PyThreadState_Detach, but since those were there already I'm not going to bother complaining about it.

@colesburycolesbury merged commit90fe325 intopython:mainSep 16, 2025
85 of 87 checks passed
@miss-islington-app
Copy link

Thanks@colesbury for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@colesburycolesbury deleted the gh-137433-stop-the-world-daemon-deadlock branchSeptember 16, 2025 08:22
miss-islington pushed a commit to miss-islington/cpython that referenced this pull requestSep 16, 2025
…ythongh-137735)There was a deadlock originally seen by Memray when a daemon threadenabled or disabled profiling while the interpreter was shutting down.I think this could also happen with garbage collection, but I haven'tseen that in practice.The daemon thread could be hung while trying acquire the global rwmutexthat prevents overlapping global and per-interpreter stop-the-world events.Since it already held the main interpreter's stop-the-world lock, italso deadlocked the main thread, which is trying to perform interpreterfinalization.Swap the order of lock acquisition to prevent this deadlock.Additionally, refactor `_PyParkingLot_Park` so that the global bucketshashtable is left in a clean state if the thread is hung in`PyEval_AcquireThread`.(cherry picked from commit90fe325)Co-authored-by: Sam Gross <colesbury@gmail.com>
@bedevere-app
Copy link

GH-138965 is a backport of this pull request to the3.14 branch.

@bedevere-appbedevere-appbot removed the needs backport to 3.14bugs and security fixes labelSep 16, 2025
encukou pushed a commit that referenced this pull requestOct 7, 2025
…gh-137735) (GH-138965)There was a deadlock originally seen by Memray when a daemon threadenabled or disabled profiling while the interpreter was shutting down.I think this could also happen with garbage collection, but I haven'tseen that in practice.The daemon thread could be hung while trying acquire the global rwmutexthat prevents overlapping global and per-interpreter stop-the-world events.Since it already held the main interpreter's stop-the-world lock, italso deadlocked the main thread, which is trying to perform interpreterfinalization.Swap the order of lock acquisition to prevent this deadlock.Additionally, refactor `_PyParkingLot_Park` so that the global bucketshashtable is left in a clean state if the thread is hung in`PyEval_AcquireThread`.(cherry picked from commit90fe325)Co-authored-by: Sam Gross <colesbury@gmail.com>
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@gpsheadgpsheadgpshead approved these changes

@ZeroIntensityZeroIntensityZeroIntensity approved these changes

@ericsnowcurrentlyericsnowcurrentlyAwaiting requested review from ericsnowcurrentlyericsnowcurrently is a code owner

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@colesbury@gpshead@ZeroIntensity

[8]ページ先頭

©2009-2025 Movatter.jp