]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-39877: Fix take_gil() for daemon threads (GH-19054)
authorVictor Stinner <vstinner@python.org>
Wed, 18 Mar 2020 02:04:33 +0000 (03:04 +0100)
committerGitHub <noreply@github.com>
Wed, 18 Mar 2020 02:04:33 +0000 (03:04 +0100)
bpo-39877, bpo-39984: If the thread must exit, don't access tstate to
prevent a potential crash: tstate memory has been freed.

Python/ceval_gil.h

index 9c051ae57b03bcdf813036b83edeb13ba5f9e7eb..f8b06ac68c914494c20c6688cb17c71ee56e3f86 100644 (file)
@@ -281,13 +281,17 @@ _ready:
     if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) {
         RESET_GIL_DROP_REQUEST(ceval);
     }
-    if (tstate->async_exc != NULL) {
+
+    int must_exit = tstate_must_exit(tstate);
+
+    /* Don't access tstate if the thread must exit */
+    if (!must_exit && tstate->async_exc != NULL) {
         _PyEval_SignalAsyncExc(ceval);
     }
 
     MUTEX_UNLOCK(gil->mutex);
 
-    if (tstate_must_exit(tstate)) {
+    if (must_exit) {
         /* bpo-36475: If Py_Finalize() has been called and tstate is not
            the thread which called Py_Finalize(), exit immediately the
            thread.