Skip to content

Conversation

miss-islington
Copy link
Contributor

@miss-islington miss-islington commented Sep 16, 2025

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 in
PyEval_AcquireThread.
(cherry picked from commit 90fe325)

Co-authored-by: Sam Gross [email protected]

…ythongh-137735)

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 in
`PyEval_AcquireThread`.
(cherry picked from commit 90fe325)

Co-authored-by: Sam Gross <[email protected]>
Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

Up to Hugo if he wants to include this in the RC.

@colesbury
Copy link
Contributor

I think this can wait until 3.14.1

@colesbury colesbury marked this pull request as draft September 16, 2025 10:04
@colesbury
Copy link
Contributor

The added test is failing because we'll need to merge:

Before landing this PR to avoid an additional deadlock

@colesbury colesbury marked this pull request as ready for review October 7, 2025 17:55
@encukou encukou merged commit e09f33e into python:3.14 Oct 7, 2025
52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants