GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — ctypes array cache can return distinct types when types are created concurrently · Issue #160978 · python/cpython Open Open Copy link Labels extension-modulesC modules in the Modules dirC modules in the Modules dirtopic-ctypestopic-free-threadingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error DescriptionBug reportOn the free-threaded build, this fails with from concurrent.futures import ThreadPoolExecutor from ctypes import c_int from threading import Barrier barrier = Barrier(8) def make_type(n): barrier.wait() return c_int * n with ThreadPoolExecutor(8) as pool: for n in range(1, 1001): types = list(pool.map(make_type, [n] * 8)) for t in types: types[0].from_param(t()) It passes on the GIL-enabled build because there isn't an opportunity for a GIL thread switch. If I instead create a type that has a Python hook, I can deterministically trigger the same race on the GIL-enabled build as well: import ctypes from concurrent.futures import ThreadPoolExecutor from threading import Barrier barrier = Barrier(2) class Meta(type(ctypes.c_int)): def __set_name__(cls, owner, name): barrier.wait(timeout=5) class C(ctypes.c_int, metaclass=Meta): pass with ThreadPoolExecutor(2) as pool: a, b = pool.map(lambda _: C * 100, range(2)) print(a is b) a.from_param(b()) This second script prints I think the fix is to rewrite how cpython/Modules/_ctypes/_ctypes.c Lines 5363 to 5417 in 4a9b981
The current implementation has a check-then-insert TOCTOU bug. I found this looking at the root cause of numpy/numpy#29943. See also #127945. CPython versions tested on:3.14, 3.15, 3.16, main Operating systems tested on:macOS (M1) Linked PRsActivity
MetadataMetadataAssigneesLabelsextension-modulesC modules in the Modules dirC modules in the Modules dirtopic-ctypestopic-free-threadingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error ProjectsMilestoneRelationshipsDevelopmentIssue actions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |