Skip to content

Commit

Permalink
[3.11] pythongh-106883 Fix deadlock in threaded application
Browse files Browse the repository at this point in the history
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.

Jira: ENTLLT-7285

Change-Id: I2548d07803fc98db8717057ae3006e6af68b2f86
  • Loading branch information
diegorusso committed Mar 28, 2024
1 parent 6ff7c13 commit 85e6715
Showing 1 changed file with 17 additions and 0 deletions.
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 comments on commit 85e6715

Please sign in to comment.