gh-109746: Make `_thread.start_new_thread` delete state of new thread on its startup failure by chgnrdv · Pull Request #109761 · python/cpython · GitHub
Skip to content
Merged
34 changes: 34 additions & 0 deletions Lib/test/test_threading.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
If :func:`!_thread.start_new_thread` fails to start a new thread, it deletes its state from interpreter and thus avoids its repeated cleanup on finalization.
1 change: 1 addition & 0 deletions Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ ThreadHandle_start(ThreadHandle *self, PyObject *func, PyObject *args,
PyThread_handle_t os_handle;
if (PyThread_start_joinable_thread(thread_run, boot, &ident, &os_handle)) {
PyThreadState_Clear(boot->tstate);
PyThreadState_Delete(boot->tstate);
thread_bootstate_free(boot, 1);
PyErr_SetString(ThreadError, "can't start new thread");
goto start_failed;
Expand Down
4 changes: 3 additions & 1 deletion Python/pystate.c