]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-128485: ensure that dlmalloc initializes itself at import time in ctypes (#131633)
authorKumar Aditya <kumaraditya@python.org>
Mon, 24 Mar 2025 12:44:25 +0000 (18:14 +0530)
committerGitHub <noreply@github.com>
Mon, 24 Mar 2025 12:44:25 +0000 (18:14 +0530)
Modules/_ctypes/_ctypes.c

index ef4d98ea56ffc6d16323e4de4fc54eae8bb2954c..2c2d09625a98254c1ec33d46f950c0b4ee2f5a97 100644 (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:
-*/