Skip to content

import ctypes sometimes fails when subinterpreters import it in parallel #163231

Backlog

Description

@dstein64

Bug 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

Activity

  1. added
    type-bug
    on Dec 6, 2026
  2. added
    stdlib
    extension-modules
    and removed
    stdlib
    on Dec 6, 2026
  3. picnixz commented on Dec 6, 2026

    @picnixz
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulestopic-ctypestopic-subinterpreterstype-bug

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions