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..57971d837c --- /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 automatic garbage-collection timing. diff --git a/policyengine_uk/tests/test_variable_module_release.py b/policyengine_uk/tests/test_variable_module_release.py index b8b07ec949..39c6193ce2 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. 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: + 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.