Skip to content

Sharing a memoryview iterator across threads can abort free-threaded CPython #159760

Open

Description

@ngoldbaum

On a free-threaded debug build, concurrently exhausting a memoryview iterator can abort with a negative refcount. The exporter is not mutated or explicitly released.

import array
import threading

N = 20_000
shared = iter(memoryview(array.array("I", range(N))))
results = [[] for _ in range(4)]
start = threading.Barrier(4)

def worker(i):
    start.wait()
    results[i].extend(shared)

threads = [threading.Thread(target=worker, args=(i,)) for i in range(4)]
for thread in threads:
    thread.start()
for thread in threads:
    thread.join()

values = [v for result in results for v in result]
print("count:", len(values), "distinct:", len(set(values)))

Run PYTHON_GIL=0 ./python.exe repro.py repeatedly to trigger the following crash:

Python/brc.c:69: _Py_NegativeRefcount: Assertion failed: object has negative ref count
Enable tracemalloc to get the memory block allocation traceback

object address  : 0x2000296d670
object refcount : 0
object type     : 0x1038bc060
object type name: memoryview
object repr     : <refcnt 0 at 0x2000296d670>

Fatal Python error: _PyObject_AssertFailed: _PyObject_AssertFailed
Python runtime state: initialized

Stack (most recent call first):
  File "/Users/goldbaum/Documents/test.py", line 11 in worker
  File "/Users/goldbaum/.pyenv/versions/3.16t-dev-debug/lib/python3.16t/threading.py", line 1160 in run
  File "/Users/goldbaum/.pyenv/versions/3.16t-dev-debug/lib/python3.16t/threading.py", line 1218 in _bootstrap_inner
  File "/Users/goldbaum/.pyenv/versions/3.16t-dev-debug/lib/python3.16t/threading.py", line 1180 in _bootstrap
[1]    61221 abort      python -Xfaulthandler test.py

With PYTHON_GIL=1 on the same binary I'm unable to trigger any crashes.

memoryiter_next() reads it_seq before clearing and decrementing it at exhaustion, so two threads can drop the iterator's single owning reference. @devdanzin reported this race earlier in a comment, with a separate reproducer and sanitizer results.

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

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

Python 3.16.0a1+dev free-threading build (main, Oct 19 2026, 11:39:38) [Clang 21.0.0 (clang-2100.3.34.2)]

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    type-crashA hard crash of the interpreter, possibly with a core dump
    on Oct 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

    stdlibStandard Library Python modules in the Lib/ directorytopic-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