"""Exercise the real pytest hooks in isolated, repeatable interpreter sessions.""" from __future__ import annotations import os from pathlib import Path import subprocess import sys import textwrap import pytest pytestmark = pytest.mark.cpu _CASES = ''' import gc import weakref import pytest import torch import lifecycle_state as state class Cycle: def __init__(self): self.link = self BASELINE = Cycle() @pytest.fixture(scope="module") def module_cycle(): value = Cycle() state.module_refs.append(weakref.ref(value)) return [value] @pytest.fixture def dependent(tiny_model_runtime, module_cycle): assert torch.get_num_threads() == 1 assert gc.isenabled() is state.gc_enabled if state.failure == "setup": raise RuntimeError("intentional setup failure") yield if state.failure == "teardown": raise RuntimeError("intentional teardown failure") def test_cycles(dependent, module_cycle): # Collection-time objects are frozen only when this session owns the freeze. if not state.external: assert not any(obj is BASELINE for obj in gc.get_objects()) # A module fixture created after collection must NOT have been frozen by # the function fixture, even though it predates that fixture's setup. module_cycle.clear() gc.collect() assert state.module_refs[-1]() is None value = Cycle() reference = weakref.ref(value) del value gc.collect() assert reference() is None state.checked_cycles += 1 if state.failure == "call": raise AssertionError("intentional call failure") if state.failure == "interrupt": raise KeyboardInterrupt def test_following_case(tiny_model_runtime): assert torch.get_num_threads() == 1 gc.collect() if state.failure != "setup": assert all(reference() is None for reference in state.module_refs) ''' _RUNNER = ''' import gc import importlib.util import json from pathlib import Path import sys import types import weakref import pytest import torch state = types.ModuleType("lifecycle_state") sys.modules[state.__name__] = state state.external = sys.argv[1] == "True" state.gc_enabled = sys.argv[2] == "True" state.checked_cycles = 0 if not state.gc_enabled: gc.disable() # Simulate a caller-owned state; the hooks must preserve it. torch.set_num_threads(4) gc.collect() original_count = gc.get_freeze_count() class Cycle: def __init__(self): self.link = self if state.external: external = Cycle() external_reference = weakref.ref(external) gc.freeze() del external original_freeze, original_unfreeze = gc.freeze, gc.unfreeze calls = [] def freeze(): calls.append("freeze") original_freeze() def unfreeze(): calls.append("unfreeze") original_unfreeze() gc.freeze, gc.unfreeze = freeze, unfreeze observations = [] try: # Repeated modules, failure paths, interruption, and repeated pytest.main # calls share one interpreter so leaked ownership cannot hide at process exit. for index, failure in enumerate(("none", "call", "setup", "teardown", "interrupt", "none")): state.failure = failure state.module_refs = [] calls.clear() result = pytest.main([ "-q", "--tb=short", "-p", "no:cacheprovider", "--basetemp", str(Path("tmp") / str(index)), "test_first.py", "test_second.py", ]) expected = 0 if failure == "none" else 2 if failure == "interrupt" else 1 assert int(result) == expected, (failure, result) assert torch.get_num_threads() == 4 assert gc.isenabled() is state.gc_enabled assert calls == ([] if state.external else ["freeze", "unfreeze"]), calls gc.collect() if state.external: assert external_reference() is not None else: assert gc.get_freeze_count() == original_count assert all(reference() is None for reference in state.module_refs) for name in ("test_first", "test_second"): module = sys.modules[name] reference = weakref.ref(module.BASELINE) module.BASELINE = module.Cycle() gc.collect() assert reference() is None, "collection-time baseline leaked after cleanup" observations.append([failure, int(result), list(calls)]) class StartupFailure: def pytest_runtestloop(self, session): raise RuntimeError("intentional startup failure after collection") calls.clear() assert pytest.main(["-q", "-p", "no:cacheprovider"], plugins=[StartupFailure()]) == 3 assert calls == ([] if state.external else ["freeze", "unfreeze"]) assert torch.get_num_threads() == 4 if not state.external: assert gc.get_freeze_count() == original_count # Collection-only must neither acquire nor release a freeze. calls.clear() assert pytest.main(["--collect-only", "-q", "-p", "no:cacheprovider"]) == 0 assert not calls # Failure inside the function fixture's setup must restore threads too. spec = importlib.util.spec_from_file_location("fixture_under_test", "conftest.py") fixture_module = importlib.util.module_from_spec(spec) spec.loader.exec_module(fixture_module) if not state.external and original_count: # Fault injection: a matching count must not authorize ownership when # the known immortal tuples are actually in ordinary generations. enabled, collect, get_count = gc.isenabled(), gc.collect, gc.get_freeze_count gc.disable() original_unfreeze() gc.collect = lambda: 0 gc.get_freeze_count = lambda: original_count try: assert not fixture_module._can_own_gc_freeze() finally: gc.collect, gc.get_freeze_count = collect, get_count gc.collect() if enabled: gc.enable() collect = gc.collect def fail_collection(): raise RuntimeError("intentional collection failure") gc.collect = fail_collection try: fixture = fixture_module.tiny_model_runtime.__wrapped__() try: next(fixture) except RuntimeError as error: assert str(error) == "intentional collection failure" else: raise AssertionError("fixture did not propagate the collection error") assert torch.get_num_threads() == 4 finally: gc.collect = collect finally: gc.freeze, gc.unfreeze = original_freeze, original_unfreeze if state.external: assert external_reference() is not None gc.unfreeze() # Only the original owner releases its objects. gc.collect() assert external_reference() is None gc.enable() assert state.checked_cycles == 9, state.checked_cycles Path("result.json").write_text(json.dumps(observations)) ''' @pytest.mark.parametrize("external,gc_enabled", [(False, True), (True, True), (False, False)]) def test_runtime_gc_and_threads_survive_sessions_and_failures(tmp_path, external, gc_enabled): # Copy the actual hooks rather than a test-specific model of their behavior. source = Path(__file__).with_name("conftest.py") (tmp_path / "conftest.py").write_text(source.read_text()) (tmp_path / "pytest.ini").write_text("[pytest]\n") for module in ("test_first.py", "test_second.py"): (tmp_path / module).write_text(textwrap.dedent(_CASES)) (tmp_path / "runner.py").write_text(textwrap.dedent(_RUNNER)) result = subprocess.run( [sys.executable, "runner.py", str(external), str(gc_enabled)], cwd=tmp_path, capture_output=True, text=True, timeout=60, env={**os.environ, "PYTEST_DISABLE_PLUGIN_AUTOLOAD": "1", "PYTEST_ADDOPTS": "", "HF_HUB_OFFLINE": "1", "TRANSFORMERS_OFFLINE": "1"}, ) assert result.returncode == 0, result.stdout + result.stderr assert (tmp_path / "result.json").is_file()