chore: fail a launcher test that leaves a process

A launcher left behind by a test kept running into the modules after it,
and once for two days after the run that started it. Each simulated
device now waits 6 s after its other cleanups for anything carrying its
FAKE_STATE, kills what is left and fails the test naming it.
This commit is contained in:
Abdessamad Derraz committed 2026-11-12 02:25:00 +01:00
1 parent 106d13add1
commit b1ff6c762e
1 file changed
+54
+54
View File
@@ -697,6 +697,8 @@ class Device:
self.root = _tempdir(test)
self.state = self.root / "state"
self.state.mkdir()
# Registered before every other cleanup, so it runs after them.
test.addCleanup(self._sweep)
self.bin = self.root / "bin"
self.bin.mkdir()
master, slave = pty.openpty()
@@ -821,6 +823,40 @@ class Device:
except (ProcessLookupError, ValueError):
pass
def _left(self) -> list[int]:
"""The processes a run on this device started that still run: each
carries the device's FAKE_STATE in its environment."""
marker = f"FAKE_STATE={self.state}".encode()
found = []
for entry in Path("/proc").iterdir():
if not entry.name.isdigit() or int(entry.name) == os.getpid():
continue
try:
environ = (entry / "environ").read_bytes()
except OSError:
continue
if marker in environ.split(b"\0"):
found.append(int(entry.name))
return found
def _sweep(self) -> None:
"""No process outlives its test. A launcher's stop sentinel polls
every 5 s, so what is still running 6 s on was left behind: it is
killed, and the test fails naming it, instead of the leak running
into the tests after it."""
end = time.monotonic() + 6
while (left := self._left()) and time.monotonic() < end:
time.sleep(0.1)
named = []
for pid in left:
try:
argv = Path(f"/proc/{pid}/cmdline").read_bytes().replace(b"\0", b" ")
os.kill(pid, signal.SIGKILL)
except (ProcessLookupError, FileNotFoundError):
continue
named.append(f"{pid} {argv.decode('utf-8', 'replace').strip()}")
self.test.assertEqual(named, [], "processes left running after the test")
def calls(self) -> list[dict]:
path = self.state / "calls.jsonl"
if not path.exists():
@@ -2024,6 +2060,24 @@ INI
device.assert_mapper_gone()
class TheSweep(unittest.TestCase):
def test_a_process_left_running_fails_its_test(self):
started = []
class Leaky(unittest.TestCase):
def runTest(inner):
device = Device(inner)
started.append(subprocess.Popen(["sleep", "60"], env=device.env,
stdin=subprocess.DEVNULL))
result = unittest.TestResult()
Leaky().run(result)
[proc] = started
self.assertEqual(proc.wait(timeout=5), -signal.SIGKILL)
[(_, report)] = result.failures
self.assertIn(f"{proc.pid} sleep 60", report)
class TheLiveness(unittest.TestCase):
def test_a_zombie_is_not_running(self):
"""A process that exited and that its parent has not reaped still