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

Deadlock at shutdown with stop-the-world and daemon threads #137433

Closed
Labels
3.13bugs and security fixes3.14bugs and security fixes3.15new features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)topic-free-threadingtype-bugAn unexpected behavior, bug, or error
@colesbury

Description

@colesbury

Bug report

Bug description:

Reported by@pablogsal /@godlygeek from memray

Stack trace:
https://gist.github.com/pablogsal/513fa8b0c29cda852ce11c86ce3b1345

We have two threads, the main thread (M) and a daemon thread (D). The main thread starts_Py_Finalize() and performs a global stop the world. The daemon thread is disabling profiling and so tries to performa a stop-the-world specific to it's interpreter:

cpython/Python/pystate.c

Lines 2256 to 2267 in9745976

staticvoid
stop_the_world(struct_stoptheworld_state*stw)
{
_PyRuntimeState*runtime=&_PyRuntime;
PyMutex_Lock(&stw->mutex);
if (stw->is_global) {
_PyRWMutex_Lock(&runtime->stoptheworld_mutex);
}
else {
_PyRWMutex_RLock(&runtime->stoptheworld_mutex);
}

M:_PyEval_StopTheWorldAll():
M: acquiresruntime->stoptheworld->mutex
M: acquires RW lockruntime->stoptheworld_mutex in W (exclusive) mode
M: ... waits on threads
D:_PyEval_StopTheWorld(interp):
D: acquiresinterp->stoptheworld->mutex
D: ... blocks trying to acquireruntime->stoptheworld_mutex in R mode. Later, the daemon thread will hang in_PyThreadState_HangThread() when trying to re-attach it's thread state.
M:_PyEval_StopTheWorldAll() finishes, marks the interpreter as finalizing
M: ...
M: calls_PyGC_CollectNoFail() which tries to run_PyEval_StopTheWorld(interp)
M: ... blocks trying to acquireinterp->stoptheworld->mutex, which is still held by the daemon thread!

Deadlock! Summary:

The daemon thread holdsinterp->stoptheworld->mutex and is hanging because the interpreter is shutting down.

The main thread is trying to perform the shutdown procedure, including calling the GC a few times, which requiresinterp->stoptheworld->mutex.

Fix???

  • Release the previously acquiredinterp->stoptheworld->mutex when hanging the thread if necessary? Crosses a bunch of abstraction barriers, which is messy and tricky

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13bugs and security fixes3.14bugs and security fixes3.15new features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)topic-free-threadingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2026 Movatter.jp