Skip to content

test_cycle in test_free_threading.test_itertools is flaky #135639

Open
@colesbury

Description

@colesbury

Bug report

Bug description:

next() on a shared itertools.cycle() iterator may raise a StopIteration. With the GIL, that would never happen because the iterator loops forever. I think that's fine -- the iterator doesn't need to be thread-safe -- but the test needs to be fixed.

test_cycle (test.test_free_threading.test_itertools.ItertoolsThreading.test_cycle) ...  Warning -- Uncaught thread exception: StopIteration
Exception in thread Thread-7130 (work):
Traceback (most recent call last):
  File "/.../checkout/Lib/threading.py", line 1074, in _bootstrap_inner
    self._context.run(self.run)
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^
  File "/.../checkout/Lib/threading.py", line 1016, in run
    self._target(*self._args, **self._kwargs)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.../checkout/Lib/test/test_free_threading/test_itertools.py", line 47, in work
    _ = next(it)
StopIteration
ok

The problem is that the underlying iterator may be exhausted:

item = PyIter_Next(lz->it);

But the other thread's may not have added anything to lz->saved yet:

if (PyList_Append(lz->saved, item)) {

Exit code path (leads to StopIteration):

if (PyList_GET_SIZE(lz->saved) == 0)
return NULL;

cc @eendebakpt @kumaraditya303

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

Metadata

Metadata

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions