From 180300c70fe8058c0aefa3ed97495ccfcc26008f Mon Sep 17 00:00:00 2001 From: Matt Page Date: Mon, 11 Mar 2024 15:47:09 -0700 Subject: [PATCH] Add handles to shutdown list before starting the thread --- Modules/_threadmodule.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c index 9f837d50cfa656..faf9b3804d1b32 100644 --- a/Modules/_threadmodule.c +++ b/Modules/_threadmodule.c @@ -1737,14 +1737,20 @@ do_start_new_thread(thread_module_state *state, PyObject *func, PyObject *args, return -1; } - if (ThreadHandle_start(handle, func, args, kwargs) < 0) { - return -1; - } - if (!daemon) { + // Add the handle before starting the thread to avoid adding a handle + // to a thread that has already finished (i.e. if the thread finishes + // before the call to `ThreadHandle_start()` below returns). add_to_shutdown_handles(state, handle); } + if (ThreadHandle_start(handle, func, args, kwargs) < 0) { + if (!daemon) { + remove_from_shutdown_handles(handle); + } + return -1; + } + return 0; }