Skip to content

Commit 29356e0

Browse files
authored
bpo-39877: Fix take_gil() for daemon threads (GH-19054)
bpo-39877, bpo-39984: If the thread must exit, don't access tstate to prevent a potential crash: tstate memory has been freed.
1 parent 23ef89d commit 29356e0

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

Python/ceval_gil.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -281,13 +281,17 @@ take_gil(PyThreadState *tstate)
281281
if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) {
282282
RESET_GIL_DROP_REQUEST(ceval);
283283
}
284-
if (tstate->async_exc != NULL) {
284+
285+
int must_exit = tstate_must_exit(tstate);
286+
287+
/* Don't access tstate if the thread must exit */
288+
if (!must_exit && tstate->async_exc != NULL) {
285289
_PyEval_SignalAsyncExc(ceval);
286290
}
287291

288292
MUTEX_UNLOCK(gil->mutex);
289293

290-
if (tstate_must_exit(tstate)) {
294+
if (must_exit) {
291295
/* bpo-36475: If Py_Finalize() has been called and tstate is not
292296
the thread which called Py_Finalize(), exit immediately the
293297
thread.

0 commit comments

Comments
 (0)