GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Sharing a `contextvars.Context` iterator across threads crashes (HAMT iterator cursor corruption) under free-threading · Issue #154535 · python/cpython Open 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 DescriptionCrash reportWhat happened?On the free-threaded build, advancing a single shared iterator over a The HAMT iterator keeps its whole cursor — static hamt_iter_t hamt_iterator_next(PyHamtIteratorState *iter, PyObject **key, PyObject **val) { if (iter->i_level < 0) /* :2188 plain read of the cursor level */ return I_END; PyHamtNode *current = iter->i_nodes[iter->i_level]; /* :2194 read node at current level */ if (IS_BITMAP_NODE(current)) { /* :2196 Py_TYPE(current) -> SEGV on a wild node */ return hamt_iterator_bitmap_next(iter, key, val); } ... } static hamt_iter_t hamt_iterator_bitmap_next(PyHamtIteratorState *iter, ...) { int8_t level = iter->i_level; PyHamtNode_Bitmap *node = (PyHamtNode_Bitmap *)(iter->i_nodes[level]); if (pos + 1 >= Py_SIZE(node)) { /* :2092 Py_SIZE(node) -> SEGV on a wild node */ iter->i_level--; /* :2097 plain write of the cursor level */ return hamt_iterator_next(iter, key, val); } ... } Two threads calling The HAMT iterator backs ReproducerFree-threaded build, import contextvars, threading NT = 8; ITERS = 40000 vs = [contextvars.ContextVar(f"v{i}") for i in range(16)] ctx = contextvars.copy_context() def populate(): for i, v in enumerate(vs): v.set(i) ctx.run(populate) cell = [iter(ctx)] def worker(): for _ in range(ITERS): it = cell[0] try: next(it) except StopIteration: cell[0] = iter(ctx) except Exception: pass ts = [threading.Thread(target=worker) for _ in range(NT)] for t in ts: t.start() for t in ts: t.join()
The crash is not Relationship to gh-124397Per gh-124397 ("Strategy for Iterators in Free Threading") point 3, C iterators should get "the minimal changes necessary to cause them to not crash … Concurrent access is allowed to return duplicate values, skip values, or raise an exception." The HAMT / Suggested fixBring the HAMT iterator advance under a per-iterator critical section (or make the Found with fusil's CPython versions tested on:CPython main branch Operating systems tested on:Linux Output from running 'python -VV' on the command line:Python 3.16.0a0 free-threading build (heads/main:a1d580430c8, Jul 18 2026, 20:23:36) [Clang 21.1.8 (6ubuntu1)] Linked PRsActivity
MetadataMetadataAssigneesLabelsinterpreter-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 ProjectsMilestoneRelationshipsDevelopmentIssue 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. |