Skip to content

Sequence iterator thread-safety #120496

Description

@rostan-t

Bug report

Bug description:

Sequence iterators are not thread-safe under the free-threaded build. They'll sometimes be accessed with the same index.

Here is a minimal repro:

importconcurrent.futuresN=10000for_inrange(100):
it=iter(range(N))
withconcurrent.futures.ThreadPoolExecutor() asexecutor:
data=set(executor.map(lambda_: next(it), range(N)))
assertlen(data) ==N, f"Expected {N} distinct elements, got {len(data)}"

I also tested it with list and dict iterators as well as a custom class implementing __getitem__.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux, macOS

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions