GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Crash when a callable iterator is shared between threads · Issue #161792 · python/cpython

Crash when a callable iterator is shared between threads #161792

New issue
Copy link
New issue
Copy link
Open
Open
Crash when a callable iterator is shared between threads#161792
Copy link
Assignees
nascheme
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-free-threadingtype-crashA hard crash of the interpreter, possibly with a core dumpA hard crash of the interpreter, possibly with a core dump

Description

@nascheme
nascheme
opened on Nov 11, 2026
Issue body actions

Crash report

What happened?

In the free-threaded build, an iter(callable, sentinel) iterator that is used from several threads can crash the interpreter.

calliter_iternext() calls it_callable and compares the result against it_sentinel through borrowed pointers. Another thread can concurrently clear those fields when the iterator is exhausted, or replace it_sentinel / it_stop_exc in __setstate__(), freeing an object that is still in use. Two threads that exhaust the iterator at the same time can also both Py_DECREF the same callable and sentinel.

Reproducer (segfaults within a few seconds on a free-threaded release build of main):

import threading

N = 2000

def worker(its, barrier):
    barrier.wait()
    for it in its:
        for _ in it:
            pass

for _ in range(200):
    # Each iterator holds the only reference to its callable.
    its = [iter(lambda: None, None) for _ in range(N)]
    barrier = threading.Barrier(4)
    threads = [threading.Thread(target=worker, args=(its, barrier))
               for _ in range(4)]
    for t in threads:
        t.start()
    for t in threads:
        t.join()

The default (GIL) build is not affected.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

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

No response

Linked PRs

  • gh-161792: Fix crashes in the callable iterator #161793

Activity

  1. nascheme
    added
    type-crashA hard crash of the interpreter, possibly with a core dumpA hard crash of the interpreter, possibly with a core dump
    topic-free-threading
    on Nov 11, 2026
  2. nascheme
    self-assigned this
    on Nov 11, 2026
  3. picnixz
    added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    on Nov 11, 2026
  4. kumaraditya303 commented on Nov 11, 2026

    @kumaraditya303
    kumaraditya303
    on Nov 11, 2026
    Contributor
    More actions

    One possible alternative to fixing this is to implement this in pure Python like I did for aiter, if there isn't too much performance slowdown.

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

Metadata

Metadata

Assignees

  • @nascheme
    nascheme

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-free-threadingtype-crashA hard crash of the interpreter, possibly with a core dumpA 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

    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.