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; }