Skip to content

SIGSEGV with generators in free-threaded build #120321

Description

@rostan-t

Crash report

What happened?

Trying to iterate on a generator from multiple threads under the free-threaded build results in a segmentation fault.

Here is a minimal repro:

import concurrent.futures


def gen():
    while True:
        yield


it = gen()
with concurrent.futures.ThreadPoolExecutor() as executor:
    while True:
        _ = executor.submit(lambda: next(it))

The issue seems to be specific to generators as other kinds of iterators work well in parallel.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Output from running 'python -VV' on the command line:

Python 3.14.0a0 experimental free-threading build (heads/main:c3b6dbff2c, Jun 10 2024, 16:54:16) [GCC 11.4.0]

Linked PRs

Activity

  1. added
    type-crashA hard crash of the interpreter, possibly with a core dump
    on Jun 10, 2024
  2. Fidget-Spinner commented on Jun 10, 2024

    @Fidget-Spinner
    Member

    Thanks for the repro. Looking into this.

  3. Fidget-Spinner commented on Jun 10, 2024

    @Fidget-Spinner
    Member

    Confirmed, with pydebug and tsan.

  4. Zheaoli commented on Jun 10, 2024

    @Zheaoli
    Contributor

    May I take a chance to dive into this issue?

  5. Fidget-Spinner commented on Jun 10, 2024

    @Fidget-Spinner
    Member

    @Zheaoli sorry I assigned myself on this one I have a fix already and will put it up shortly. Do you have any specific issues in mind you would like to work on? I could help mentor/guide you on stuff you're interested in if I can help!

  6. Zheaoli commented on Jun 10, 2024

    @Zheaoli
    Contributor

    sorry I assigned myself on this one I have a fix already and will put it up shortly

    No problem!

    Do you have any specific issues in mind you would like to work on? I could help mentor/guide you on stuff you're interested in if I can help!

    For now, I'm a fresh man try to be more familiar with the CPython codebase.

    I'm now working on #104280 to make dtrace more functional to help debug the process. I'm also interested in JIT and Tier2(especially to enhance the observerability for the Python process). But I'm still trying to find the issue I can fix and the thing I can do(lol

    Thanks for knowing that you will be willing to mentor/guide me lol!

  7. Fidget-Spinner commented on Jun 11, 2024

    @Fidget-Spinner
    Member

    @Zheaoli please send me an email I'll help give some pointers privately

  8. removed their assignment
    on Nov 15, 2024
  9. added a commit that references this issue on Mar 13, 2025
  10. added 2 commits that reference this issue on Mar 27, 2025
  11. devdanzin commented on Apr 21, 2025

    @devdanzin
    Member

    fusil has hit this one during fuzzing, it results in different segfaults and an abort, here's the MRE I got:

    from threading import Thread
    
    gen = (x for x in range(1_000_000))
    def run_gen():
        for n in gen:
            pass
    
    [Thread(target=run_gen, args=()).start() for x in range(20)]
  12. 37 remaining items

  13. added a commit that references this issue on Feb 3, 2026
  14. added 4 commits that reference this issue on Feb 15, 2026
  15. sergey-miryanov commented on May 4, 2026

    @sergey-miryanov
    Contributor

    This fails with asserts on current 3.14:

    Assertion failed: frame == &entry.frame, file X:\Work\cpython\3.14\Python\generated_cases.c.h, line 7694
    

    And works well on current main.

    Should we close this now since it's not planned for backport?

  16. kumaraditya303 commented on Aug 1, 2026

    @kumaraditya303
    Contributor

    Reopening for async generators which are still not thread safe.

  17. added a commit that references this issue on Aug 19, 2026
  18. added 3 commits that reference this issue on Aug 19, 2026
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

    3.15bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)topic-free-threadingtype-crashA hard crash of the interpreter, possibly with a core dump

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions