https://github.com/python/cpython/commit/04d4aacaacbe26146e9b5ff6b0043f86f3c543d4
commit: 04d4aacaacbe26146e9b5ff6b0043f86f3c543d4
branch: main
author: Kumar Aditya <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2025-03-24T18:14:25+05:30
summary:
gh-128485: ensure that dlmalloc initializes itself at import time in ctypes
(#131633)
files:
M Modules/_ctypes/_ctypes.c
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index ef4d98ea56ffc6..2c2d09625a9825 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -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:
-*/
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]