Title: ctypes array cache can return distinct types when types are created concurrently · Issue #160978 · python/cpython · GitHub
Open Graph Title: ctypes array cache can return distinct types when types are created concurrently · Issue #160978 · python/cpython
X Title: ctypes array cache can return distinct types when types are created concurrently · Issue #160978 · python/cpython
Description: Bug report On the free-threaded build, this fails with TypeError: expected c_int_Array_1 instance instead of c_int_Array_1: from concurrent.futures import ThreadPoolExecutor from ctypes import c_int from threading import Barrier barrier ...
Open Graph Description: Bug report On the free-threaded build, this fails with TypeError: expected c_int_Array_1 instance instead of c_int_Array_1: from concurrent.futures import ThreadPoolExecutor from ctypes import c_in...
X Description: Bug report On the free-threaded build, this fails with TypeError: expected c_int_Array_1 instance instead of c_int_Array_1: from concurrent.futures import ThreadPoolExecutor from ctypes import c_in...
Opengraph URL: https://github.com/python/cpython/issues/160978
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"ctypes array cache can return distinct types when types are created concurrently","articleBody":"### Bug report\n\nOn the free-threaded build, this fails with `TypeError: expected c_int_Array_1 instance instead of c_int_Array_1`:\n\n```python\nfrom concurrent.futures import ThreadPoolExecutor\nfrom ctypes import c_int\nfrom threading import Barrier\n\nbarrier = Barrier(8)\n\ndef make_type(n):\n barrier.wait()\n return c_int * n\n\nwith ThreadPoolExecutor(8) as pool:\n for n in range(1, 1001):\n types = list(pool.map(make_type, [n] * 8))\n for t in types:\n types[0].from_param(t())\n```\n\nIt passes on the GIL-enabled build because there isn't an opportunity for a GIL thread switch.\n\nIf I instead create a type that has a Python hook, I can deterministically trigger the same race on the GIL-enabled build as well:\n\n```python\nimport ctypes\nfrom concurrent.futures import ThreadPoolExecutor\nfrom threading import Barrier\n\nbarrier = Barrier(2)\n\nclass Meta(type(ctypes.c_int)):\n def __set_name__(cls, owner, name):\n barrier.wait(timeout=5)\n\nclass C(ctypes.c_int, metaclass=Meta):\n pass\n\nwith ThreadPoolExecutor(2) as pool:\n a, b = pool.map(lambda _: C * 100, range(2))\n\nprint(a is b)\na.from_param(b())\n```\n\nThis second script prints `False` and then `TypeError: expected C_Array_100 instance instead of C_Array_100` on both builds, because defining `__set_name__` makes python code run after the cache miss on the array type cache during creation of the array class.\n\nI think the fix is to rewrite how `PyCArrayType_from_ctype` uses the `array_cache`: https://github.com/python/cpython/blob/4a9b98115fec4d93c32c554aa541d6c3fc4c3b27/Modules/_ctypes/_ctypes.c#L5363-L5417\n\nThe current implementation has a check-then-insert TOCTOU bug.\n\nI found this looking at the root cause of https://github.com/numpy/numpy/issues/29943. See also #127945.\n\n### CPython versions tested on:\n\n3.14, 3.15, 3.16, main\n\n### Operating systems tested on:\n\nmacOS (M1)\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-161494\n* gh-162312\n* gh-162313\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/ngoldbaum","@type":"Person","name":"ngoldbaum"},"datePublished":"2026-10-30T16:26:54Z","url":"https://github.com/python/cpython/issues/160978"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:a864e014-70d9-1fb7-cb5c-b37a60b56a71 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D36E:9E08E:506293:652E46:6B020267 |
| html-safe-nonce | eca23b9f986f18ea51445e824e7d30053c5cb6bec48321754289335439f10b3c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMzZFOjlFMDhFOjUwNjI5Mzo2NTJFNDY6NkIwMjAyNjciLCJ2aXNpdG9yX2lkIjoiMzM1NTAyNTI0OTkzNzc4NTQ0NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 7525e036babcde458a5a3f2ed7e51c10cbc96bf7736e2c2e927798e8e05a0095 |
| hovercard-subject-tag | issue:6122912621 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/160978/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ffec0cd3728df5de67216e7949700b2d7558a9902a2a6d51a0ad31df47e76106/python/cpython/issues/160978 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ffec0cd3728df5de67216e7949700b2d7558a9902a2a6d51a0ad31df47e76106/python/cpython/issues/160978 |
| og:image:alt | Bug report On the free-threaded build, this fails with TypeError: expected c_int_Array_1 instance instead of c_int_Array_1: from concurrent.futures import ThreadPoolExecutor from ctypes import c_in... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ngoldbaum |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width