Skip to content

Commit fd00239

Browse files
ensure that dlmalloc initializes itself at import time
1 parent ef06508 commit fd00239

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

Modules/_ctypes/_ctypes.c

+12-6
Original file line numberDiff line numberDiff line change
@@ -6062,6 +6062,18 @@ _ctypes_add_objects(PyObject *mod)
60626062
static int
60636063
_ctypes_mod_exec(PyObject *mod)
60646064
{
6065+
// See https://github.com/python/cpython/issues/128485
6066+
// This allocates some memory and then frees it to ensure that the
6067+
// the dlmalloc allocator initializes itself to avoid data races
6068+
// in free-threading.
6069+
void *codeloc = NULL;
6070+
void *ptr = Py_ffi_closure_alloc(sizeof(void *), &codeloc);
6071+
if (ptr == NULL) {
6072+
PyErr_NoMemory();
6073+
return -1;
6074+
}
6075+
Py_ffi_closure_free(ptr);
6076+
60656077
ctypes_state *st = get_module_state(mod);
60666078
st->_unpickle = PyObject_GetAttrString(mod, "_unpickle");
60676079
if (st->_unpickle == NULL) {
@@ -6189,9 +6201,3 @@ PyInit__ctypes(void)
61896201
{
61906202
return PyModuleDef_Init(&_ctypesmodule);
61916203
}
6192-
6193-
/*
6194-
Local Variables:
6195-
compile-command: "cd .. && python setup.py -q build -g && python setup.py -q build install --home ~"
6196-
End:
6197-
*/

0 commit comments

Comments
 (0)