2
2
/* Thread and interpreter state structures and their interfaces */
3
3
4
4
#include "Python.h"
5
- #include "objimpl.h"
6
5
#include "pycore_ceval.h"
7
6
#include "pycore_code.h" // stats
8
7
#include "pycore_frame.h"
@@ -1389,10 +1388,6 @@ PyThreadState_Next(PyThreadState *tstate) {
1389
1388
PyObject *
1390
1389
_PyThread_CurrentFrames (void )
1391
1390
{
1392
- // Disable the GC as this can cause a deadlock the interpreter.
1393
- // See issues 116969 and 106883.
1394
- PyGC_Disable ();
1395
-
1396
1391
PyThreadState * tstate = _PyThreadState_GET ();
1397
1392
if (_PySys_Audit (tstate , "sys._current_frames" , NULL ) < 0 ) {
1398
1393
return NULL ;
@@ -1403,6 +1398,9 @@ _PyThread_CurrentFrames(void)
1403
1398
return NULL ;
1404
1399
}
1405
1400
1401
+ // gh-106883: Disable the GC as this can cause the interpreter to deadlock
1402
+ int gc_was_enabled = PyGC_Disable ();
1403
+
1406
1404
/* for i in all interpreters:
1407
1405
* for t in all of i's thread states:
1408
1406
* if t's frame isn't NULL, map t's id to its frame
@@ -1446,19 +1444,17 @@ _PyThread_CurrentFrames(void)
1446
1444
done :
1447
1445
HEAD_UNLOCK (runtime );
1448
1446
1449
- // Once we release the runtime, the GC can be reenabled.
1450
- PyGC_Enable ();
1447
+ // Once the runtime is released, the GC can be reenabled.
1448
+ if (gc_was_enabled ) {
1449
+ PyGC_Enable ();
1450
+ }
1451
1451
1452
1452
return result ;
1453
1453
}
1454
1454
1455
1455
PyObject *
1456
1456
_PyThread_CurrentExceptions (void )
1457
1457
{
1458
- // Disable the GC as this can cause a deadlock the interpreter.
1459
- // See issues 116969 and 106883.
1460
- PyGC_Disable ();
1461
-
1462
1458
PyThreadState * tstate = _PyThreadState_GET ();
1463
1459
1464
1460
_Py_EnsureTstateNotNULL (tstate );
@@ -1472,6 +1468,9 @@ _PyThread_CurrentExceptions(void)
1472
1468
return NULL ;
1473
1469
}
1474
1470
1471
+ // gh-106883: Disable the GC as this can cause the interpreter to deadlock
1472
+ int gc_was_enabled = PyGC_Disable ();
1473
+
1475
1474
/* for i in all interpreters:
1476
1475
* for t in all of i's thread states:
1477
1476
* if t's frame isn't NULL, map t's id to its frame
@@ -1513,8 +1512,10 @@ _PyThread_CurrentExceptions(void)
1513
1512
done :
1514
1513
HEAD_UNLOCK (runtime );
1515
1514
1516
- // Once we release the runtime, the GC can be reenabled.
1517
- PyGC_Enable ();
1515
+ // Once the runtime is released, the GC can be reenabled.
1516
+ if (gc_was_enabled ) {
1517
+ PyGC_Enable ();
1518
+ }
1518
1519
1519
1520
return result ;
1520
1521
}
0 commit comments