mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
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:
parent
b61fece852
commit
e545ead66c
5 changed files with 160 additions and 73 deletions
|
@ -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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue