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
@rhettinger The issue that this PR solves is the following: when defining the iterator as repeat(data, 245) and concurrent iteration with multiple threads in this part of the code:
if (ro->cnt > 0)
ro->cnt--;
we can have multiple threads passing the condition ro->cnt > 0 (if ro->cnt is 1), and then decrementing ro->cnt to a negative value. The iterator will then nevert be exhausted (even though this is not the intend of the user).
A test for this is:
import unittest
from threading import Thread, Barrier
from itertools import repeat
from test.support import threading_helper
threading_helper.requires_working_threading(module=True)
class ItertoolsThreading(unittest.TestCase):
@threading_helper.reap_threads
def test_repeat(self):
number_of_threads = 20
number_of_iterations = 700_000
barrier = Barrier(number_of_threads)
def work(it):
barrier.wait()
while True:
try:
_ = next(it)
except StopIteration:
break
data = 3
for it in range(number_of_iterations):
print(f'iteration {it}')
repeat_iterator = repeat(data, 245)
worker_threads = []
for ii in range(number_of_threads):
worker_threads.append(
Thread(target=work, args=[repeat_iterator]))
with threading_helper.start_threads(worker_threads):
pass
barrier.reset()
if __name__ == "__main__":
unittest.main()
On my system this creates a deadlock (typically after a couple of 1000) iterations in a free-threading debug build.
Summary of the results of the build (if available):
==
Click to see traceback logs
Traceback (most recent call last):
File "/Users/buildbot/Library/Developer/XCTestDevices/7817F54E-F383-435E-A96B-7848C5D4910E/data/Containers/Bundle/Application/6FD8422A-BA2D-42A0-9F8A-786BED06BB0E/iOSTestbed.app/python/lib/python3.14/threading.py", line 1079, in _bootstrap_innerself._context.run(self.run)
~~~~~~~~~~~~~~~~~^^^^^^^^^^
File "/Users/buildbot/Library/Developer/XCTestDevices/7817F54E-F383-435E-A96B-7848C5D4910E/data/Containers/Bundle/Application/6FD8422A-BA2D-42A0-9F8A-786BED06BB0E/iOSTestbed.app/python/lib/python3.14/threading.py", line 1021, in runself._target(*self._args, **self._kwargs)
~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/buildbot/Library/Developer/XCTestDevices/7817F54E-F383-435E-A96B-7848C5D4910E/data/Containers/Bundle/Application/6FD8422A-BA2D-42A0-9F8A-786BED06BB0E/iOSTestbed.app/python/lib/python3.14/test/test_interpreters/test_stress.py", line 30, in task
interp = interpreters.create()
File "/Users/buildbot/Library/Developer/XCTestDevices/7817F54E-F383-435E-A96B-7848C5D4910E/data/Containers/Bundle/Application/6FD8422A-BA2D-42A0-9F8A-786BED06BB0E/iOSTestbed.app/python/lib/python3.14/test/support/interpreters/__init__.py", line 76, in createid= _interpreters.create(reqrefs=True)
interpreters.InterpreterError: interpreter creation failed
k
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
If needed we can add a unit test similar to the one in https://github.com/python/cpython/pull/131212/files