Skip to content

Commit

Permalink
_thread.start_new_thread: allocate/free thread bootstate using raw me…
Browse files Browse the repository at this point in the history
…mory allocator

When new thread is starting, but Python is finalizing, PyMem_Free can't be called on new thread bootstate, because this created thread doesn't own the GIL. We should use raw memory allocator to allocate/free bootstate to avoid crashes/other unexpected behaviour.
  • Loading branch information
chgnrdv committed Sep 24, 2023
1 parent e81bd3f commit 4e1d6df
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 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,13 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
return NULL;
}

struct bootstate *boot = PyMem_NEW(struct bootstate, 1);
struct bootstate *boot = PyMem_RawMalloc(sizeof(struct bootstate));
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

0 comments on commit 4e1d6df

Please sign in to comment.