Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit eb97914

Browse files
committed
Fix race in test_trashcan_threads in test_gc.py
See #52
1 parent ec46739 commit eb97914

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

Lib/test/test_gc.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import_module)
77
from test.support.script_helper import assert_python_ok, make_script
88

9+
import _atomic
910
import gc
1011
import sys
1112
import sysconfig
@@ -378,15 +379,15 @@ def sleeper_gen():
378379
time.sleep(0.000001)
379380

380381
class C(list):
381-
# Appending to a list is atomic, which avoids the use of a lock.
382-
inits = []
383-
dels = []
382+
inits = _atomic.int()
383+
dels = _atomic.int()
384+
384385
def __init__(self, alist):
385386
self[:] = alist
386-
C.inits.append(None)
387+
C.inits.add(1)
387388
def __del__(self):
388389
# This __del__ is called by subtype_dealloc().
389-
C.dels.append(None)
390+
C.dels.add(1)
390391
# `g` will release the GIL when garbage-collected. This
391392
# helps assert subtype_dealloc's behaviour when threads
392393
# switch in the middle of it.
@@ -405,23 +406,23 @@ def make_nested():
405406

406407
def run_thread():
407408
"""Exercise make_nested() in a loop."""
408-
while not exit:
409+
while exit.load() == 0:
409410
make_nested()
410411

411412
old_switchinterval = sys.getswitchinterval()
412413
sys.setswitchinterval(1e-5)
413414
try:
414-
exit = []
415+
exit = _atomic.int()
415416
threads = []
416417
for i in range(N_THREADS):
417418
t = threading.Thread(target=run_thread)
418419
threads.append(t)
419-
with start_threads(threads, lambda: exit.append(1)):
420+
with start_threads(threads, lambda: exit.store(1)):
420421
time.sleep(1.0)
421422
finally:
422423
sys.setswitchinterval(old_switchinterval)
423424
gc.collect()
424-
self.assertEqual(len(C.inits), len(C.dels))
425+
self.assertEqual(C.inits.load(), C.dels.load())
425426

426427
def test_boom(self):
427428
class Boom:

0 commit comments

Comments
 (0)