Skip to content
Prev Previous commit
test iter_reduce and iter_setstate
  • Loading branch information
tom-pytel committed Feb 18, 2025
commit 095e20e8aa7cf8b346c4231bc3c12b7a6461670e
12 changes: 11 additions & 1 deletion Lib/test/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2521,12 +2521,20 @@ def check(funcs, a=None, *args):
@threading_helper.requires_working_threading()
def test_free_threading_bytearrayiter(self):
# Non-deterministic but good chance to fail if bytearrayiter is not free-threading safe.
# We are fishing for a "Assertion failed: object has negative ref count".
# We are fishing for a "Assertion failed: object has negative ref count" and tsan races.

def iter_next(b, it):
b.wait()
list(it)

Comment thread
tom-pytel marked this conversation as resolved.
def iter_reduce(b, it):
b.wait()
it.__reduce__()

def iter_setstate(b, it):
b.wait()
it.__setstate__(0)

def check(funcs, it):
barrier = threading.Barrier(len(funcs))
threads = []
Expand All @@ -2543,6 +2551,8 @@ def check(funcs, it):
ba = bytearray(b'0' * 0x4000) # this is a load-bearing variable, do not remove

check([iter_next] * 10, iter(ba))
check([iter_next] + [iter_reduce] * 10, iter(ba)) # for tsan
check([iter_next] + [iter_setstate] * 10, iter(ba)) # for tsan


if __name__ == "__main__":
Expand Down