From b1ff6c762e46d4ab409a0f9339bec81b0943b915 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Thu, 12 Nov 2026 02:25:00 +0100 Subject: [PATCH] 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. --- tests/test_handheld_launchers.py | 54 ++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tests/test_handheld_launchers.py b/tests/test_handheld_launchers.py index bf77786c5..b69af08f7 100644 --- a/tests/test_handheld_launchers.py +++ b/tests/test_handheld_launchers.py @@ -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