Skip to content

Commit 87e8870

Browse files
committed
This approach sort of works.
1 parent d820ba2 commit 87e8870

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

Python/ceval.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2467,21 +2467,28 @@ PyEval_SetTraceAllThreads(Py_tracefunc func, PyObject *arg)
24672467
_PyRuntimeState *runtime = &_PyRuntime;
24682468
HEAD_LOCK(runtime);
24692469
PyThreadState* ts = PyInterpreterState_ThreadHead(interp);
2470-
/* gh-132296: We need to prevent the thread state
2471-
from getting concurrently deallocated.
2472-
We can't stop-the-world because _PyEval_SetTrace() is re-entrant. */
2470+
/* gh-132296: We need to prevent the thread state from being concurrently
2471+
deallocated. We can't stop-the-world because _PyEval_SetTrace()
2472+
is re-entrant. */
24732473
_PyThreadState_Incref(ts);
24742474
HEAD_UNLOCK(runtime);
24752475

2476-
while (ts) {
2476+
while (ts != NULL) {
2477+
/*
24772478
if (_PyEval_SetTrace(ts, func, arg) < 0) {
24782479
PyErr_FormatUnraisable("Exception ignored in PyEval_SetTraceAllThreads");
24792480
}
2481+
*/
24802482
HEAD_LOCK(runtime);
24812483
PyThreadState *old = ts;
24822484
ts = PyThreadState_Next(ts);
2483-
_PyThreadState_Decref(old);
2485+
/* Drop the reference to the prior thread state
2486+
and acquire a reference to the next one. */
2487+
if (ts != NULL) {
2488+
_PyThreadState_Incref(ts);
2489+
}
24842490
HEAD_UNLOCK(runtime);
2491+
_PyThreadState_Decref(old);
24852492
}
24862493
}
24872494

Python/pystate.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1898,7 +1898,7 @@ PyThreadState_Delete(PyThreadState *tstate)
18981898
_Py_EnsureTstateNotNULL(tstate);
18991899
tstate_verify_not_active(tstate);
19001900
tstate_delete_common(tstate, 0);
1901-
free_threadstate((_PyThreadStateImpl *)tstate);
1901+
decref_threadstate((_PyThreadStateImpl *)tstate);
19021902
}
19031903

19041904

@@ -1911,7 +1911,7 @@ _PyThreadState_DeleteCurrent(PyThreadState *tstate)
19111911
#endif
19121912
current_fast_clear(tstate->interp->runtime);
19131913
tstate_delete_common(tstate, 1); // release GIL as part of call
1914-
free_threadstate((_PyThreadStateImpl *)tstate);
1914+
decref_threadstate((_PyThreadStateImpl *)tstate);
19151915
}
19161916

19171917
void

0 commit comments

Comments
 (0)