8000 [3.11] gh-106883 Fix deadlock in threaded application by diegorusso · Pull Request #117332 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.11] gh-106883 Fix deadlock in threaded application #117332

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 5 commits into from
Mar 11, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[3.11] gh-106883 Fix deadlock in threaded application
When using threaded applications, there is a high risk of a deadlock in
the intepreter.
It's a lock ordering deadlock with HEAD_LOCK(&_PyRuntime); and the GIL.

It has been suggested to disable GC during the
_PyThread_CurrentFrames() and _PyThread_CurrentExceptions() calls.
  • Loading branch information
diegorusso committed Mar 28, 2024
commit 183aef0b3e1eddbb76d813db639ab0a4d496c0ac
17 changes: 17 additions & 0 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* Thread and interpreter state structures and their interfaces */

#include "Python.h"
#include "objimpl.h"
#include "pycore_ceval.h"
#include "pycore_code.h" // stats
#include "pycore_frame.h"
Expand Down Expand Up @@ -1388,6 +1389,10 @@ PyThreadState_Next(PyThreadState *tstate) {
PyObject *
_PyThread_CurrentFrames(void)
{
// Disable the GC as this can cause a deadlock the interpreter.
// See issues 116969 and 106883.
PyGC_Disable();

PyThreadState *tstate = _PyThreadState_GET();
if (_PySys_Audit(tstate, "sys._current_frames", NULL) < 0) {
return NULL;
Expand Down Expand Up @@ -1440,12 +1445,20 @@ _PyThread_CurrentFrames(void)

done:
HEAD_UNLOCK(runtime);

// Once we release the runtime, the GC can be reenabled.
PyGC_Enable();

return result;
}

PyObject *
_PyThread_CurrentExceptions(void)
{
// Disable the GC as this can cause a deadlock the interpreter.
// See issues 116969 and 106883.
PyGC_Disable();

PyThreadState *tstate = _PyThreadState_GET();

_Py_EnsureTstateNotNULL(tstate);
Expand Down Expand Up @@ -1499,6 +1512,10 @@ _PyThread_CurrentExceptions(void)

done:
HEAD_UNLOCK(runtime);

// Once we release the runtime, the GC can be reenabled.
PyGC_Enable();

return result;
}

Expand Down
0