8000 [3.11] gh-106883 Fix deadlock in threaded application · diegorusso/cpython@9937d7e · GitHub
[go: up one dir, main page]

Skip to content

Commit 9937d7e

Browse files
committed
[3.11] pythongh-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. Jira: ENTLLT-7285 Change-Id: I2548d07803fc98db8717057ae3006e6af68b2f86
1 parent 65a0923 commit 9937d7e

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

Python/pystate.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/* Thread and interpreter state structures and their interfaces */
33

44
#include "Python.h"
5+
#include "objimpl.h"
56
#include "pycore_ceval.h"
67
#include "pycore_code.h" // stats
78
#include "pycore_frame.h"
@@ -1388,6 +1389,10 @@ PyThreadState_Next(PyThreadState *tstate) {
13881389
PyObject *
13891390
_PyThread_CurrentFrames(void)
13901391
{
1392+
// Disable the GC as this can cause a deadlock the interpreter.
1393+
// See issues 116969 and 106883.
1394+
PyGC_Disable();
1395+
13911396
PyThreadState *tstate = _PyThreadState_GET();
13921397
if (_PySys_Audit(tstate, "sys._current_frames", NULL) < 0) {
13931398
return NULL;
@@ -1440,12 +1445,20 @@ _PyThread_CurrentFrames(void)
14401445

14411446
done:
14421447
HEAD_UNLOCK(runtime);
1448+
1449+
// Once we release the runtime, the GC can be reenabled.
1450+
PyGC_Enable();
1451+
14431452
return result;
14441453
}
14451454

14461455
PyObject *
14471456
_PyThread_CurrentExceptions(void)
14481457
{
1458+
// Disable the GC as this can cause a deadlock the interpreter.
1459+
// See issues 116969 and 106883.
1460+
PyGC_Disable();
1461+
14491462
PyThreadState *tstate = _PyThreadState_GET();
14501463

14511464
_Py_EnsureTstateNotNULL(tstate);
@@ -1499,6 +1512,10 @@ _PyThread_CurrentExceptions(void)
14991512

15001513
done:
15011514
HEAD_UNLOCK(runtime);
1515+
1516+
// Once we release the runtime, the GC can be reenabled.
1517+
PyGC_Enable();
1518+
15021519
return result;
15031520
}
15041521

0 commit comments

Comments
 (0)
0