Drain garbage collector before reference cycle tests.
When tests run in the same pytest worker, earlier tests can leave behind
objects with weak-reference callbacks or finalizers (such as interned shaped
arrays). Clearing JAX caches drops strong references to those objects, but a
single gc.collect() pass cleans up the first tier of objects and executes
their callbacks. Those callbacks can drop references to cyclic garbage, which
is collected on a subsequent gc.collect() pass.
If a single gc.collect() runs before enabling gc.DEBUG_SAVEALL, leftover
cyclic garbage from earlier tests is captured and fails the test.
Run gc.collect() in a bounded loop at the start of reference cycle tests so
leftover garbage from preceding tests is drained before testing.
This should fix the following CI failure:
https://github.com/jax-ml/jax/actions/runs/33137705370/job/98743238595
```
tests/core_test.py:309: in test_reference_cycles
self.assertEqual(gc.collect(), 0, msg=str(gc.garbage))
E AssertionError: 64 != 0 : [b, 134753695081696 created at /__w/jax/jax/tests/shape_poly_test.py:1381:17 (PolyHarness.run_test), ((b, 1),), [], (b, 1), b, ((b, 1),), (b, 1), b, {b: (1, inf, <BoundsPrecision.FOR_GEQ0_OR_LT0: 1>), b - 1: (0, inf, <BoundsPrecision.FOR_GEQ0_OR_LT0: 1>)}, (1, inf, <BoundsPrecision.FOR_GEQ0_OR_LT0: 1>), b - 1, ((b, 1), (, -1)), (0, inf, <BoundsPrecision.FOR_GEQ0_OR_LT0: 1>), (, -1), (b, 1), b, 134753697930640 created at /__w/jax/jax/tests/shape_poly_test.py:1381:17 (PolyHarness.run_test), ((b, 1),), [], (b, 1), b, ((b, 1),), (b, 1), b, {b: (1, inf, <BoundsPrecision.FOR_GEQ0_OR_LT0: 1>), b - 1: (0, inf, <BoundsPrecision.FOR_GEQ0_OR_LT0: 1>)}, (1, inf, <BoundsPrecision.FOR_GEQ0_OR_LT0: 1>), b - 1, ((b, 1), (, -1)), (0, inf, <BoundsPrecision.FOR_GEQ0_OR_LT0: 1>), (, -1), (b, 1), b, 134771042588032 created at /__w/jax/jax/tests/shape_poly_test.py:1381:17 (PolyHarness.run_test), ((b, 1),), [], (b, 1), b, ((b, 1),), (b, 1), b, {b: (1, inf, <BoundsPrecision.FOR_GEQ0_OR_LT0: 1>), b - 1: (0, inf, <BoundsPrecision.FOR_GEQ0_OR_LT0: 1>)}, (1, inf, <BoundsPrecision.FOR_GEQ0_OR_LT0: 1>), b - 1, ((b, 1), (, -1)), (0, inf, <BoundsPrecision.FOR_GEQ0_OR_LT0: 1>), (, -1), (b, 1), b, 134773995355072 created at /__w/jax/jax/tests/shape_poly_test.py:1381:17 (PolyHarness.run_test), ((b, 1),), [], {b: (1, inf, <BoundsPrecision.FOR_GEQ0_OR_LT0: 1>), b - 1: (0, inf, <BoundsPrecision.FOR_GEQ0_OR_LT0: 1>)}, (b, 1), (1, inf, <BoundsPrecision.FOR_GEQ0_OR_LT0: 1>), (0, inf, <BoundsPrecision.FOR_GEQ0_OR_LT0: 1>), b - 1, b, ((b, 1), (, -1)), ((b, 1),), (, -1), (b, 1), (b, 1), b]
```
PiperOrigin-RevId: 972648359 P
Peter Hawkins committed
1214954d0731cfde1cb7bcb2670d3abc07bb5a42
Parent: b277e8e
Committed by jax authors <google-ml-automation@google.com>
on 8/28/2026, 5:02:16 PM