test_doxygen_reap.py¶
tests/test_doxygen_reap.py
An abandoned doxygen run must not outlive the call that started it.
test_an_aborted_doxygen_run_leaves_no_survivor
None test_doxygen_reap.test_an_aborted_doxygen_run_leaves_no_survivor(Path tmp_path, pytest.MonkeyPatch monkeypatch)
An exception during the run must not leave the child behind.
tests/test_doxygen_reap.py:114
Checks
assert _survivors(stub_dir), "the stub never spawned; this test would be vacuous"assert not leaked, (f"doxygen child(ren) {leaked} survived an aborted run. In the field this accumulated " "multi-GB processes parented to the MCP server, which then competed with the next " "build attempt for the machine." )
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CLI-002 | inferred | test_an_aborted_doxygen_run_leaves_no_survivor → run_doxygen → doxygen_supports_sqlite3 |
| REQ-DDB-INDEX-001 | measured | test_an_aborted_doxygen_run_leaves_no_survivor → run_doxygen |
| REQ-DDB-PIPE-003 | measured | test_an_aborted_doxygen_run_leaves_no_survivor → run_doxygen → _build_doxyfile_content → extra_input_clears_exclude |
| REQ-DDB-PIPE-006 | measured | test_an_aborted_doxygen_run_leaves_no_survivor → run_doxygen → clean_subprocess_env |
Source · 61 lines
def test_an_aborted_doxygen_run_leaves_no_survivor(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""FAILS BEFORE THE FIX with the stub still running. The abort is raised from inside the
output reader, which is where a real abandonment lands: that loop is what the call is sitting
in for the whole of a long doxygen run, so it is the realistic place for a cancellation, a
KeyboardInterrupt, or any other unwind to arrive.
`BaseException` rather than `Exception` on purpose — the interesting cases (KeyboardInterrupt,
SystemExit, anyio's cancellation) are all outside `Exception`, and a `finally` that only
survives `Exception` would leak on exactly the paths that matter.
@brief An unwinding run reaps its child.
@version 1
"""
stub_dir = _write_stub(tmp_path)
monkeypatch.setenv("PATH", f"{stub_dir}{os.pathsep}{os.environ['PATH']}")
def _abort(*_args: object, **_kwargs: object) -> None:
"""WAITS FOR THE GRANDCHILD BEFORE ABORTING, and without that this test passes against
completely unfixed code. Raising the instant the reader is entered kills the stub shell
BEFORE it has forked its `sleep`, so there is nothing to leak and a pid-only kill looks
indistinguishable from a process-group kill. Verified with a probe: the same stub leaks a
`sleep` under `proc.kill()` when given a second to get there, and leaks nothing when
killed immediately.
That is the shape of a real abandonment too — the call is abandoned some way into a long
doxygen run, not in the microsecond after spawn.
"""
deadline = time.monotonic() + 10.0
while time.monotonic() < deadline and not _survivors(stub_dir):
time.sleep(0.05)
assert _survivors(stub_dir), "the stub never spawned; this test would be vacuous"
raise KeyboardInterrupt("abandoned mid-run")
monkeypatch.setattr(doxygen, "_consume_doxygen_output", _abort)
try:
with pytest.raises(BaseException):
doxygen.run_doxygen(tmp_path / "Doxyfile", tmp_path, output_dir=tmp_path / "out")
## The child is killed asynchronously; give the kernel a moment to reap it before
## concluding it survived. A bare check here would be flaky in the passing direction,
## which is the worse direction for a leak test.
deadline = time.monotonic() + 5.0
while time.monotonic() < deadline and _survivors(stub_dir):
time.sleep(0.1)
leaked = _survivors(stub_dir)
assert not leaked, (
f"doxygen child(ren) {leaked} survived an aborted run. In the field this accumulated "
"multi-GB processes parented to the MCP server, which then competed with the next "
"build attempt for the machine."
)
finally:
## Never leave a 3600 s sleeper behind, whatever the assertion did.
for pid in _survivors(stub_dir):
try:
os.kill(pid, signal.SIGKILL)
except (ProcessLookupError, PermissionError):
pass1 more lines, not shown