You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Single-threaded re-entrancy via a recursive generator causes an access violation (segfault) in pairwise_next #149557
The existing partial fix in main (the it = po->it; if (it == NULL) re-read guard after the first tp_iternext call) does not cover the second tp_iternext call, where it is still a borrowed reference. In the single-threaded case a re-entrant call can trigger Py_CLEAR(po->it) which drops the generator's refcount to zero and frees it, leaving the outer call's local it as a dangling pointer.
Concurrent use of count is also common, because it is atomic. This is an idiomatic way to generate unique consequent integers.
Many of other generators, like product or combinations can also be used concurrently to distribute work between threads. If we make filterfalse, starmap or zip_longest non-reentrant, we should do this for filter, map and zip too. I afraid this can break user code.
If we implemented these iterators in Python, there would not be leaks or crashes. Therefore, it is possible to implement them without leaks and crashes.
Activity
eendebakpt commentedon May 8, 2026
A reproducer for
batched:The
isliceis vulnerable as well.Ping @rhettinger is code owner for itertools.
rhettinger commentedon May 19, 2026
Let's make all of the itertools (except for
repeatbehave like generators and raise an exception if called while running.pythongh-149557: Raise RuntimeError on re-entrant itertools.pairwise.…
serhiy-storchaka commentedon May 29, 2026
Concurrent use of
countis also common, because it is atomic. This is an idiomatic way to generate unique consequent integers.Many of other generators, like
productorcombinationscan also be used concurrently to distribute work between threads. If we makefilterfalse,starmaporzip_longestnon-reentrant, we should do this forfilter,mapandziptoo. I afraid this can break user code.If we implemented these iterators in Python, there would not be leaks or crashes. Therefore, it is possible to implement them without leaks and crashes.
pythongh-149557, pythongh-123471: Make itertools.pairwise safe under …
pythongh-149557, pythongh-123471: Make itertools.pairwise safe under …