Skip to content

Commit 514f81d

Browse files
committed
Add a better test case.
1 parent 498222b commit 514f81d

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

Lib/test/test_free_threading/test_gc.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,38 @@ def mutator_thread():
6262
with threading_helper.start_threads(gcs + mutators):
6363
pass
6464

65+
def test_freeze_object_in_brc_queue(self):
66+
# GH-142975: Freezing objects in the BRC queue could result in some
67+
# objects having a zero refcount without being deallocated.
68+
69+
class Weird:
70+
# We need a destructor to trigger the check for object resurrection
71+
def __del__(self):
72+
pass
73+
74+
# This is owned by the main thread, so the subthread will have to increment
75+
# this object's reference count.
76+
weird = Weird()
77+
78+
def evil():
79+
gc.freeze()
80+
81+
# Decrement the reference count from this thread, which will trigger the
82+
# slow path during resurrection and add our weird object to the BRC queue.
83+
nonlocal weird
84+
del weird
85+
86+
# Collection will merge the object's reference count and make it zero.
87+
gc.collect()
88+
89+
# Unfreeze the object, making it visible to the GC.
90+
gc.unfreeze()
91+
gc.collect()
92+
93+
thread = Thread(target=evil)
94+
thread.start()
95+
thread.join()
96+
6597

6698
if __name__ == "__main__":
6799
unittest.main()

Lib/test/test_gc.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,18 +1249,6 @@ def test_tuple_untrack_counts(self):
12491249
self.assertTrue(new_count - count > (n // 2))
12501250

12511251

1252-
@threading_helper.requires_working_threading()
1253-
def test_concurrent_freeze_unfreeze():
1254-
# GH-142975: On the free-threaded build, this would cause problems
1255-
# with objects that had a per-thread reference count.
1256-
def weird():
1257-
gc.freeze()
1258-
gc.collect()
1259-
gc.unfreeze()
1260-
1261-
threading_helper.run_concurrently(weird, 4)
1262-
1263-
12641252
class IncrementalGCTests(unittest.TestCase):
12651253
@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
12661254
@requires_gil_enabled("Free threading does not support incremental GC")

0 commit comments

Comments
 (0)