Skip to content

Commit 391f7c1

Browse files
kumaraditya303diegorusso
authored andcommitted
pythongh-128485: ensure that dlmalloc initializes itself at import time in ctypes (python#131633)
1 parent c192bf4 commit 391f7c1

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

Modules/_ctypes/_ctypes.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6070,6 +6070,18 @@ _ctypes_add_objects(PyObject *mod)
60706070
static int
60716071
_ctypes_mod_exec(PyObject *mod)
60726072
{
6073+
// See https://github.com/python/cpython/issues/128485
6074+
// This allocates some memory and then frees it to ensure that the
6075+
// the dlmalloc allocator initializes itself to avoid data races
6076+
// in free-threading.
6077+
void *codeloc = NULL;
6078+
void *ptr = Py_ffi_closure_alloc(sizeof(void *), &codeloc);
6079+
if (ptr == NULL) {
6080+
PyErr_NoMemory();
6081+
return -1;
6082+
}
6083+
Py_ffi_closure_free(ptr);
6084+
60736085
ctypes_state *st = get_module_state(mod);
60746086
st->_unpickle = PyObject_GetAttrString(mod, "_unpickle");
60756087
if (st->_unpickle == NULL) {
@@ -6197,9 +6209,3 @@ PyInit__ctypes(void)
61976209
{
61986210
return PyModuleDef_Init(&_ctypesmodule);
61996211
}
6200-
6201-
/*
6202-
Local Variables:
6203-
compile-command: "cd .. && python setup.py -q build -g && python setup.py -q build install --home ~"
6204-
End:
6205-
*/

0 commit comments

Comments
 (0)