`import ctypes` sometimes fails when subinterpreters import it in parallel · Issue #163231 · python/cpython
Repository navigation
import ctypes sometimes fails when subinterpreters import it in parallel #163231
New issue
New issue
Backlog
Backlog
import ctypes sometimes fails when subinterpreters import it in parallel #163231
Labels
extension-modules
topic-ctypes
topic-subinterpreters
type-bug
Description
@dstein64's profile dstein64 opened on Dec 6, 2026Bug report
Importing ctypes in several subinterpreters at the same time, each in its own thread, sometimes fails with an AttributeError about a missing _type_ attribute.
from concurrent import interpreters
from concurrent.futures import ThreadPoolExecutor
def run(_):
interp = interpreters.create()
interp.exec('import ctypes')
interp.close()
with ThreadPoolExecutor(16) as executor:
list(executor.map(run, range(1600)))
Running this script often fails, though not every time. With ThreadPoolExecutor(1), so only one subinterpreter imports at a time, it doesn't fail.
Example error:
Traceback (most recent call last):
File "<script>", line 1, in <module>
File ".../lib/python3.15/ctypes/__init__.py", line 235, in <module>
class c_ubyte(_SimpleCData):
_type_ = "B"
AttributeError: class must define a '_type_' attribute which must be
one of these characters: 'cbBhHiIlLdfuzZqQPOv?g',
or one of these strings: 'Zf', 'Zd', 'Zg'.
I encounter the issue on Linux. It didn't fail on macOS in 5 runs.
CPython versions tested on:
3.15
Operating systems tested on:
Linux
Metadata
Metadata
Assignees
No one assignedLabels
extension-modules
topic-ctypes
topic-subinterpreters
type-bug
Projects
- Subinterpreters Status Todo