File tree Expand file tree Collapse file tree 1 file changed +18
-1
lines changed Expand file tree Collapse file tree 1 file changed +18
-1
lines changed Original file line number Diff line number Diff line change @@ -213,8 +213,13 @@ take_gil(PyThreadState *tstate)
213
213
214
214
assert (tstate != NULL );
215
215
216
- /* Check if we should make a quick exit. */
217
216
if (tstate_must_exit (tstate )) {
217
+ /* bpo-39877: If Py_Finalize() has been called and tstate is not the
218
+ thread which called Py_Finalize(), exit immediately the thread.
219
+
220
+ This code path can be reached by a daemon thread after Py_Finalize()
221
+ completes. In this case, tstate is a dangling pointer: points to
222
+ PyThreadState freed memory. */
218
223
PyThread_exit_thread ();
219
224
}
220
225
@@ -282,6 +287,18 @@ take_gil(PyThreadState *tstate)
282
287
283
288
MUTEX_UNLOCK (gil -> mutex );
284
289
290
+ if (tstate_must_exit (tstate )) {
291
+ /* bpo-36475: If Py_Finalize() has been called and tstate is not
292
+ the thread which called Py_Finalize(), exit immediately the
293
+ thread.
294
+
295
+ This code path can be reached by a daemon thread which was waiting
296
+ in take_gil() while the main thread called
297
+ wait_for_thread_shutdown() from Py_Finalize(). */
298
+ drop_gil (ceval , tstate );
299
+ PyThread_exit_thread ();
300
+ }
301
+
285
302
errno = err ;
286
303
}
287
304
You can’t perform that action at this time.
0 commit comments