gh-125859: Fix crash when gc.get_objects is called during GC (#125882)

This fixes a crash when `gc.get_objects()` or `gc.get_referrers()` is
called during a GC in the free threading build.

Switch to `_PyObjectStack` to avoid corrupting the `struct worklist`
linked list maintained by the GC. Also, don't return objects that are frozen
(`gc.freeze()`) or in the process of being collected to more closely match
the behavior of the default build.
This commit is contained in:
Sam Gross 2024-10-24 09:33:11 -04:00 committed by GitHub
parent b61fece852
commit e545ead66c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 160 additions and 73 deletions

View file

@ -1065,6 +1065,29 @@ class GCTests(unittest.TestCase):
self.assertEqual(len(gc.get_referents(untracked_capsule)), 0)
gc.get_referents(tracked_capsule)
@cpython_only
def test_get_objects_during_gc(self):
# gh-125859: Calling gc.get_objects() or gc.get_referrers() during a
# collection should not crash.
test = self
collected = False
class GetObjectsOnDel:
def __del__(self):
nonlocal collected
collected = True
objs = gc.get_objects()
# NB: can't use "in" here because some objects override __eq__
for obj in objs:
test.assertTrue(obj is not self)
test.assertEqual(gc.get_referrers(self), [])
obj = GetObjectsOnDel()
obj.cycle = obj
del obj
gc.collect()
self.assertTrue(collected)
class IncrementalGCTests(unittest.TestCase):