From 0afae8928bbe723e9603366745d8ed16f05bb0b8 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 25 Sep 2023 18:12:43 +0200 Subject: [PATCH] =?UTF-8?q?[3.11]=20gh-109795:=20`=5Fthread.start=5Fnew=5F?= =?UTF-8?q?thread`:=20allocate=20thread=20bootstate=20usin=E2=80=A6=20(#10?= =?UTF-8?q?9852)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit gh-109795: `_thread.start_new_thread`: allocate thread bootstate using raw memory allocator (#109808) (cherry picked from commit 1b8f2366b38c87b0450d9c15bdfdd4c4a2fc3a01) Co-authored-by: Radislav Chugunov <52372310+chgnrdv@users.noreply.github.com> --- Modules/_threadmodule.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c index ac49ee5c35d..72eda4917ab 100644 --- a/Modules/_threadmodule.c +++ b/Modules/_threadmodule.c @@ -1068,7 +1068,7 @@ thread_bootstate_free(struct bootstate *boot, int decref) Py_DECREF(boot->args); Py_XDECREF(boot->kwargs); } - PyMem_Free(boot); + PyMem_RawFree(boot); } @@ -1164,13 +1164,16 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs) return NULL; } - struct bootstate *boot = PyMem_NEW(struct bootstate, 1); + // gh-109795: Use PyMem_RawMalloc() instead of PyMem_Malloc(), + // because it should be possible to call thread_bootstate_free() + // without holding the GIL. + struct bootstate *boot = PyMem_RawMalloc(sizeof(struct bootstate)); if (boot == NULL) { return PyErr_NoMemory(); } boot->tstate = _PyThreadState_Prealloc(interp); if (boot->tstate == NULL) { - PyMem_Free(boot); + PyMem_RawFree(boot); return PyErr_NoMemory(); } boot->func = Py_NewRef(func);