Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:func:`!_thread.start_new_thread` now uses raw memory allocator to allocate new thread bootstate. This helps to avoid crashes in case when new thread gets started at interpreter finalization.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. Please remove this Changelog entry. I don't think that it's worth it. It doesn't impact directly users and it's a recent regression, no?

10 changes: 7 additions & 3 deletions Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@ thread_bootstate_free(struct bootstate *boot, int decref)
Py_DECREF(boot->args);
Py_XDECREF(boot->kwargs);
}
PyMem_Free(boot);
PyMem_RawFree(boot);
}


Expand Down Expand Up @@ -1184,13 +1184,17 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
return NULL;
}

struct bootstate *boot = PyMem_NEW(struct bootstate, 1);
// gh-109795: Use PyMem_RawMalloc() to allocate new thread bootstate.
// If new thread is started at Python finalization,
// i.e. thread_run() routine is invoked in newly created OS thread,
// it should be able to free bootstate without holding the GIL.
Comment thread
chgnrdv marked this conversation as resolved.
Outdated
struct bootstate *boot = PyMem_RawMalloc(sizeof(struct bootstate));
Comment thread
vstinner marked this conversation as resolved.
if (boot == NULL) {
return PyErr_NoMemory();
}
boot->tstate = _PyThreadState_New(interp);
if (boot->tstate == NULL) {
PyMem_Free(boot);
PyMem_RawFree(boot);
if (!PyErr_Occurred()) {
return PyErr_NoMemory();
}
Expand Down