Open devdanzin opened 2 weeks ago
It looks like this is some sort of 3.14 regression. On 3.13, it doesn't crash; instead, it just spams RuntimeError
s (which might also be a bug, but let's focus on this first).
This is probably a data race, but that scares me because nearly everything here is pure Python. Valgrind is pointing at native generators being the culprit, I'm looking into it.
Yeah, there's a thread safety issue with native generators (or possibly with list
). Here's a smaller reproducer:
from threading import Thread
def my_generator():
for i in range(100000):
yield i
def main(gen):
list(gen)
gener = my_generator()
ts = [Thread(target=main, args=(gener,)) for _ in range(1000)]
for t in ts:
t.start()
for t in ts:
t.join()
I had to split the fix up into two PRs because of backporting: gh-126369 and gh-126371
See also https://github.com/python/cpython/issues/120321 issue and https://github.com/python/cpython/pull/120327 fix.
Yeah, Sam mentioned that in my (closed) PR. The main concern with merging gh-120327 is that it will have a heavy performance hit on generators. IIRC, the relation with generators in this issue is that collections.abc.Iterable
(which ChainMap
inherits from) uses a generator in its __iter__
.
What happened?
The code below, in a no-gil debug build with
PYTHON_GIL=0
, results in the following abort:Found using fusil by @vstinner.
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Output from running 'python -VV' on the command line:
Python 3.14.0a1+ experimental free-threading build (heads/main:556dc9b8a7, Nov 3 2024, 10:09:47) [GCC 11.4.0]
Linked PRs