https://github.com/python/cpython/commit/c0f3fcf33681b3f5c5d50e7a301abf1eca257a4c
commit: c0f3fcf33681b3f5c5d50e7a301abf1eca257a4c
branch: main
author: Thales Moura <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2026-11-20T15:18:05+05:30
summary:
gh-160978: Return the same ctypes array type when created concurrently (#161494)
files:
A Misc/NEWS.d/next/Library/2026-11-06-12-00-00.gh-issue-160978.Kx7mQa.rst
M Lib/test/test_ctypes/test_arrays.py
M Modules/_ctypes/_ctypes.c
diff --git a/Lib/test/test_ctypes/test_arrays.py
b/Lib/test/test_ctypes/test_arrays.py
index 7f1f6cf58402c9f..5b7708afdaa1249 100644
--- a/Lib/test/test_ctypes/test_arrays.py
+++ b/Lib/test/test_ctypes/test_arrays.py
@@ -1,11 +1,14 @@
import ctypes
import sys
+import threading
import unittest
+from concurrent.futures import ThreadPoolExecutor
from ctypes import (Structure, Array, ARRAY, sizeof, addressof,
create_string_buffer, create_unicode_buffer,
c_char, c_wchar, c_byte, c_ubyte, c_short, c_ushort,
c_int, c_uint,
c_long, c_ulonglong, c_float, c_double, c_longdouble)
-from test.support import bigmemtest, _2G, threading_helper, Py_GIL_DISABLED
+from test.support import (bigmemtest, _2G, threading_helper,
+ Py_GIL_DISABLED, SHORT_TIMEOUT)
from ._support import (_CData, PyCArrayType, Py_TPFLAGS_DISALLOW_INSTANTIATION,
Py_TPFLAGS_IMMUTABLETYPE)
@@ -288,5 +291,49 @@ def run():
raise cm.exc_value
+class ArrayTypeCacheTest(unittest.TestCase):
+ # gh-160978: the array type cache could hand out distinct type objects for
+ # the same (item type, length) pair when the array type was created from
+ # more than one thread at a time.
+
+ @threading_helper.requires_working_threading()
+ def test_concurrent_creation_with_set_name(self):
+ # Creating the array type runs Python code -- ``__set_name__`` here --
+ # which lets a second thread reach the cache before the first one has
+ # stored its type. Without the fix this fails on the default build as
+ # well, so it guards the fix even where threads cannot interleave
+ # freely.
+ barrier = threading.Barrier(2)
+
+ class Meta(type(c_int)):
+ def __set_name__(cls, owner, name):
+ barrier.wait(timeout=SHORT_TIMEOUT)
+
+ class Item(c_int, metaclass=Meta):
+ pass
+
+ with ThreadPoolExecutor(2) as pool:
+ first, second = pool.map(lambda _: Item * 100, range(2))
+
+ self.assertIs(first, second)
+ # Raises TypeError if the two types are distinct.
+ first.from_param(second())
+
+ @threading_helper.requires_working_threading()
+ def test_concurrent_creation_returns_identical_types(self):
+ nthreads = 8
+ barrier = threading.Barrier(nthreads)
+
+ def make_type(length):
+ barrier.wait(timeout=SHORT_TIMEOUT)
+ return c_int * length
+
+ with ThreadPoolExecutor(nthreads) as pool:
+ for length in range(1, 51):
+ types = list(pool.map(make_type, [length] * nthreads))
+ barrier.reset()
+ self.assertEqual(len(set(types)), 1)
+
+
if __name__ == '__main__':
unittest.main()
diff --git
a/Misc/NEWS.d/next/Library/2026-11-06-12-00-00.gh-issue-160978.Kx7mQa.rst
b/Misc/NEWS.d/next/Library/2026-11-06-12-00-00.gh-issue-160978.Kx7mQa.rst
new file mode 100644
index 000000000000000..be5cc945f4eb61d
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-11-06-12-00-00.gh-issue-160978.Kx7mQa.rst
@@ -0,0 +1,3 @@
+Fix :mod:`ctypes` handing out distinct array types for the same item type
+and length when the array type is created concurrently from several
+threads.
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index 81fb9c9f57b998b..14614dca1c02cee 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -186,7 +186,23 @@ _DictRemover_call(PyObject *myself, PyObject *args,
PyObject *kw)
{
DictRemoverObject *self = _DictRemoverObject_CAST(myself);
if (self->key && self->dict) {
- if (-1 == PyDict_DelItem(self->dict, self->key)) {
+ /* Only remove the entry if it is still the proxy that died: the
+ key may have been reused for a new object in the meantime. */
+ PyObject *proxy = PyTuple_GET_SIZE(args) == 1
+ ? PyTuple_GET_ITEM(args, 0) : NULL;
+ PyObject *current;
+ int rc;
+ Py_BEGIN_CRITICAL_SECTION(self->dict);
+ rc = PyDict_GetItemRef(self->dict, self->key, ¤t);
+ if (rc == 1) {
+ rc = 0;
+ if (current == proxy) {
+ rc = PyDict_DelItem(self->dict, self->key);
+ }
+ Py_DECREF(current);
+ }
+ Py_END_CRITICAL_SECTION();
+ if (rc < 0) {
PyErr_FormatUnraisable("Exception ignored while "
"calling _ctypes.DictRemover");
}
@@ -215,32 +231,72 @@ static PyType_Spec dictremover_spec = {
.slots = dictremover_slots,
};
-int
-PyDict_SetItemProxy(ctypes_state *st, PyObject *dict, PyObject *key, PyObject
*item)
+/* Cache `item` in `dict` under `key` through a weakref proxy, unless a live
+ entry already exists. On success *presult is a new reference to whatever
+ is cached for `key` afterwards: `item` itself, or the object another
+ thread cached first.
+ Returns 0 on success, -1 with an exception set on failure. */
+static int
+_PyDict_SetDefaultItemProxy(ctypes_state *st, PyObject *dict, PyObject *key,
+ PyObject *item, PyObject **presult)
{
- PyObject *obj;
- DictRemoverObject *remover;
- PyObject *proxy;
- int result;
-
- obj = _PyObject_CallNoArgs((PyObject *)st->DictRemover_Type);
- if (obj == NULL)
+ PyObject *obj = _PyObject_CallNoArgs((PyObject *)st->DictRemover_Type);
+ if (obj == NULL) {
return -1;
+ }
- remover = (DictRemoverObject *)obj;
+ DictRemoverObject *remover = (DictRemoverObject *)obj;
assert(remover->key == NULL);
assert(remover->dict == NULL);
remover->key = Py_NewRef(key);
remover->dict = Py_NewRef(dict);
- proxy = PyWeakref_NewProxy(item, obj);
+ PyObject *proxy = PyWeakref_NewProxy(item, obj);
Py_DECREF(obj);
- if (proxy == NULL)
+ if (proxy == NULL) {
+ return -1;
+ }
+
+ PyObject *stored;
+ int present = PyDict_SetDefaultRef(dict, key, proxy, &stored);
+ if (present < 0) {
+ Py_DECREF(proxy);
return -1;
+ }
+ if (present == 0) {
+ /* Our proxy was inserted; `item` is the canonical object. */
+ Py_DECREF(stored);
+ Py_DECREF(proxy);
+ *presult = Py_NewRef(item);
+ return 0;
+ }
- result = PyDict_SetItem(dict, key, proxy);
+ /* Another thread cached an object for this key first. Prefer it, so that
+ every caller gets the same object. */
+ PyObject *existing = NULL;
+ int rc = 0;
+ if (PyWeakref_CheckProxy(stored)) {
+ rc = PyWeakref_GetRef(stored, &existing);
+ }
+ Py_DECREF(stored);
+ if (rc < 0) {
+ Py_DECREF(proxy);
+ return -1;
+ }
+ if (existing != NULL) {
+ Py_DECREF(proxy);
+ *presult = existing;
+ return 0;
+ }
+
+ /* The cached entry had already been cleared; install ours instead. */
+ rc = PyDict_SetItem(dict, key, proxy);
Py_DECREF(proxy);
- return result;
+ if (rc < 0) {
+ return -1;
+ }
+ *presult = Py_NewRef(item);
+ return 0;
}
static int
@@ -5434,13 +5490,20 @@ PyCArrayType_from_ctype(ctypes_state *st, PyObject
*itemtype, Py_ssize_t length)
Py_DECREF(key);
return NULL;
}
- if (PyDict_SetItemProxy(st, st->array_cache, key, result) < 0) {
+ PyObject *cached;
+ int rc;
+ Py_BEGIN_CRITICAL_SECTION(st->array_cache);
+ rc = _PyDict_SetDefaultItemProxy(st, st->array_cache, key, result,
+ &cached);
+ Py_END_CRITICAL_SECTION();
+ if (rc < 0) {
Py_DECREF(key);
Py_DECREF(result);
return NULL;
}
Py_DECREF(key);
- return result;
+ Py_DECREF(result);
+ return cached;
}
_______________________________________________
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]