From d8759e40c5acd8b7a885270508a0522188bc1ba8 Mon Sep 17 00:00:00 2001 From: Max Ghenis Date: Wed, 28 Oct 2026 10:58:37 -0400 Subject: [PATCH 1/3] Build the module-release test's systems with automatic GC off Module names carry id(system). A collection during the loop lets a later system reuse a dead one's address and overwrite its modules instead of adding an owner, so no release falls due and the test fails although nothing leaked. This failed pe-uk#2092's merge-queue run (40698045501, Test / Python 4/6). Keep automatic collection off while the systems are built; the release still collects. Co-Authored-By: Claude Opus 5.5 --- changelog.d/module-release-test-gc.fixed.md | 1 + .../tests/test_variable_module_release.py | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 changelog.d/module-release-test-gc.fixed.md diff --git a/changelog.d/module-release-test-gc.fixed.md b/changelog.d/module-release-test-gc.fixed.md new file mode 100644 index 0000000000..4718cc68a8 --- /dev/null +++ b/changelog.d/module-release-test-gc.fixed.md @@ -0,0 +1 @@ +Made the test that dead systems' variable modules are released during a test independent of when Python collects garbage. diff --git a/policyengine_uk/tests/test_variable_module_release.py b/policyengine_uk/tests/test_variable_module_release.py index b8b07ec949..6cbb826c01 100644 --- a/policyengine_uk/tests/test_variable_module_release.py +++ b/policyengine_uk/tests/test_variable_module_release.py @@ -138,9 +138,21 @@ def test_dead_systems_modules_are_released_while_a_test_runs(request, monkeypatc lambda **kwargs: scans.append(kwargs) or release(**kwargs), ) owners = [] - for _ in range(max_new + 3): - Simulation(situation=SITUATION).calculate("income_tax", 2026) - owners.append(len(conftest._variable_module_owners())) + # Module names carry id(system), an address. If a collection frees a dead + # system during the loop, a later system can reuse its address and + # overwrite its modules rather than add an owner, so no release falls due: + # with a collection after each build, owners stopped at live + 6 and no + # release ran. Building with automatic collection off keeps the addresses + # distinct; the release still collects. + collecting = gc.isenabled() + gc.disable() + try: + for _ in range(max_new + 3): + Simulation(situation=SITUATION).calculate("income_tax", 2026) + owners.append(len(conftest._variable_module_owners())) + finally: + if collecting: + gc.enable() # Released as each new system starts adding its variables, so at most # max_new dead systems' modules beyond the live ones, plus the newest # system's. Without the release the last count would be live + 11. From 6a826107d55659e4909615124060ddeeb93c8da8 Mon Sep 17 00:00:00 2001 From: Max Ghenis Date: Wed, 28 Oct 2026 11:05:44 -0400 Subject: [PATCH 2/3] Say what automatic GC being off does in the module-release test Co-Authored-By: Claude Opus 5.5 --- policyengine_uk/tests/test_variable_module_release.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/policyengine_uk/tests/test_variable_module_release.py b/policyengine_uk/tests/test_variable_module_release.py index 6cbb826c01..39c6193ce2 100644 --- a/policyengine_uk/tests/test_variable_module_release.py +++ b/policyengine_uk/tests/test_variable_module_release.py @@ -142,8 +142,8 @@ def test_dead_systems_modules_are_released_while_a_test_runs(request, monkeypatc # system during the loop, a later system can reuse its address and # overwrite its modules rather than add an owner, so no release falls due: # with a collection after each build, owners stopped at live + 6 and no - # release ran. Building with automatic collection off keeps the addresses - # distinct; the release still collects. + # release ran. With automatic collection off, the dead systems wait for the + # release's own collection, so each build adds an owner. collecting = gc.isenabled() gc.disable() try: From 979024824e2df8e9b71d044d7bd48b743aeadc43 Mon Sep 17 00:00:00 2001 From: Max Ghenis Date: Wed, 28 Oct 2026 13:52:50 -0400 Subject: [PATCH 3/3] Say the module-release fix covers automatic collection timing Co-Authored-By: Claude Opus 5.5 --- changelog.d/module-release-test-gc.fixed.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.d/module-release-test-gc.fixed.md b/changelog.d/module-release-test-gc.fixed.md index 4718cc68a8..57971d837c 100644 --- a/changelog.d/module-release-test-gc.fixed.md +++ b/changelog.d/module-release-test-gc.fixed.md @@ -1 +1 @@ -Made the test that dead systems' variable modules are released during a test independent of when Python collects garbage. +Made the test that dead systems' variable modules are released during a test independent of automatic garbage-collection timing.