Skip to content

Commit 7e5d9ba

Browse files
committed
test: flush GraalPy's inline caches before judging collectability (#1963)
test_cache_is_per_class_and_collectable failed on GraalPy 25.4.4 in sharded runs. The class is not freed late: GraalPy's attribute and call nodes along the nested-type walk (cls.mro() among them) cache the classes they have seen and hold them strongly until the node goes megamorphic, so a class measured while a node still has room stays reachable however long gc.collect is given. Unsharded runs passed because earlier tests had already overflowed those caches. On GraalPy only, when the first collection leaves the class alive, send throwaway classes of the same shape through the walk and collect again. A reinstated module-level WeakKeyDictionary pin still fails on both interpreters. CPython runs the check unchanged.
1 parent 861985e commit 7e5d9ba

1 file changed

Lines changed: 37 additions & 1 deletion

File tree

‎tests/corekit/test_from_dict_roundtrip_unit.py‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,46 @@
2424
"""
2525

2626
import gc
27+
import sys
2728
import unittest
2829

2930
from tests._support import reimport_once_per_class
3031

31-
#: Test tier (#1958): hand-picked round-trip cases, not a sweep.
32+
#: Test tier (#1958): hand-picked round-trip cases, not a sweep, and a
33+
#: collectability check that branches on the interpreter.
3234
__tier__ = 'ALL'
3335

36+
#: Throwaway classes :func:`release_inline_caches` sends through
37+
#: :func:`~pcapkit.corekit.infoclass._nested_info_types` before a class's
38+
#: collectability is judged. GraalPy's attribute and call nodes cache the classes
39+
#: they have seen as receivers and hold them strongly until more arrive than the
40+
#: cache has room for, so a class measured while a node in that walk still has
41+
#: room stays reachable whatever :mod:`pcapkit` does, and however long
42+
#: :func:`gc.collect` is given (#1963). CPython keeps no such cache, so it gets none.
43+
INLINE_CACHE_FLUSH = 64 if sys.implementation.name == 'graalpy' else 0
44+
45+
46+
def release_inline_caches() -> None:
47+
"""Overflow the interpreter's inline caches along the nested-type walk.
48+
49+
Each throwaway class is shaped like the one under test, so it takes the same
50+
path through :func:`~pcapkit.corekit.infoclass.info_final` and
51+
:func:`~pcapkit.corekit.infoclass._nested_info_types`. A strong reference
52+
:mod:`pcapkit` itself keeps does not let go of the class however many pass.
53+
54+
"""
55+
from pcapkit.corekit.infoclass import Info, _nested_info_types, info_final
56+
57+
for _ in range(INLINE_CACHE_FLUSH):
58+
@info_final
59+
class Throwaway(Info):
60+
nxt: int
61+
62+
Throwaway.__annotations__['nxt'] = Throwaway
63+
_nested_info_types(Throwaway)
64+
del Throwaway
65+
66+
3467
#: PCAP-NG blocks, little-endian, one per block schema that claims a type code.
3568
#: Each nests its body in :attr:`PCAPNG.block
3669
#: <pcapkit.protocols.schema.misc.pcapng.PCAPNG.block>`, which is the
@@ -256,6 +289,9 @@ class Node(Info):
256289
ref = weakref.ref(Node)
257290
del Node
258291
gc.collect()
292+
if INLINE_CACHE_FLUSH and ref() is not None:
293+
release_inline_caches()
294+
gc.collect()
259295
self.assertIsNone(ref())
260296

261297
def test_a_subclass_does_not_inherit_its_parents_map(self) -> None:

0 commit comments

Comments
 (0)