Skip to content

Commit c2b7dbe

Browse files
committed
add gc callback mutation race regression test
1 parent 2306df8 commit c2b7dbe

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

Lib/test/test_free_threading/test_gc.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import threading
44
from threading import Thread
5+
import time
56
from unittest import TestCase
67
import gc
78

@@ -94,6 +95,27 @@ def evil():
9495
thread.start()
9596
thread.join()
9697

98+
def test_gc_callbacks_race_with_mutation(self):
99+
def collect():
100+
b.wait()
101+
while not stop.is_set():
102+
gc.collect()
103+
104+
def mutate():
105+
b.wait()
106+
while not stop.is_set():
107+
gc.callbacks[:] = [lambda *_: _ for _ in range(16)]
108+
time.sleep(0)
109+
gc.callbacks.clear()
110+
111+
threads = [threading.Thread(target=f) for f in (collect, mutate) * 4]
112+
b = threading.Barrier(len(threads) + 1)
113+
stop = threading.Event()
114+
115+
with threading_helper.start_threads(threads, stop.set):
116+
b.wait()
117+
time.sleep(0.2)
118+
97119

98120
if __name__ == "__main__":
99121
unittest.main()

0 commit comments

Comments
 (0)