gh-128485: ensure that dlmalloc initializes itself at import time in ctypes (#131633)

This commit is contained in:
Kumar Aditya 2025-03-24 18:14:25 +05:30 committed by GitHub
parent 9c4fb92e12
commit 04d4aacaac
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -6070,6 +6070,18 @@ _ctypes_add_objects(PyObject *mod)
static int
_ctypes_mod_exec(PyObject *mod)
{
// See https://github.com/python/cpython/issues/128485
// This allocates some memory and then frees it to ensure that the
// the dlmalloc allocator initializes itself to avoid data races
// in free-threading.
void *codeloc = NULL;
void *ptr = Py_ffi_closure_alloc(sizeof(void *), &codeloc);
if (ptr == NULL) {
PyErr_NoMemory();
return -1;
}
Py_ffi_closure_free(ptr);
ctypes_state *st = get_module_state(mod);
st->_unpickle = PyObject_GetAttrString(mod, "_unpickle");
if (st->_unpickle == NULL) {
@ -6197,9 +6209,3 @@ PyInit__ctypes(void)
{
return PyModuleDef_Init(&_ctypesmodule);
}
/*
Local Variables:
compile-command: "cd .. && python setup.py -q build -g && python setup.py -q build install --home ~"
End:
*/