From 90b54deb9bf602fa488adf83011a789ee0637adb Mon Sep 17 00:00:00 2001 From: bong-water-water-bong Date: Sun, 15 Nov 2026 08:52:38 -0400 Subject: [PATCH] tests: the fake backend's impostor dies with it, so the port is free again The FAKE_BACKEND_IMPOSTOR mode detaches a listener (setsid, double fork) and writes its pid to a file. Killing the backend therefore does not reap it and it keeps the port for the rest of the suite -- measured on 2026-11-15 on strixhalo: parent killed, before: LISTEN 0 5 127.0.0.1:PORT users:(("python3",pid=...,fd=3)) still there parent killed, after: no listener on that port A test that starts a server must not leave one behind. smoke_serve.sh now also reads back the pid the mode already writes and asserts it is gone, so the leak cannot come back silently. --- tests/fake_backend.py | 13 +++++++++++++ tests/smoke_serve.sh | 7 +++++++ 2 files changed, 20 insertions(+) diff --git a/tests/fake_backend.py b/tests/fake_backend.py index 81e072fd0..1dd68e116 100755 --- a/tests/fake_backend.py +++ b/tests/fake_backend.py @@ -231,9 +231,22 @@ def do_POST(self): if os.environ.get("FAKE_BACKEND_IMPOSTOR"): # A process serve did not start answers on the port instead: this one detaches a server # (writing its pid to $FAKE_BACKEND_IMPOSTOR) and stays alive without listening itself. + root = os.getpid() # the process the tests kill; the setsid'd grandchild is not in its group if os.fork() == 0: os.setsid() if os.fork() == 0: + # Die with the backend that spawned this impostor. Without this the detached listener outlives + # `kill "$pid"; wait "$pid"` and keeps the port for the rest of the suite: measured on 2026-11-15, + # LISTEN 0 5 127.0.0.1:PORT survived a kill -TERM of the parent. A test that starts a server must not + # leave one behind, and the pid this impostor writes is only read by the test that asked for it. + def die_with(pid: int) -> None: + while True: + try: + os.kill(pid, 0) + except OSError: + os._exit(0) + time.sleep(0.2) + threading.Thread(target=die_with, args=(root,), daemon=True).start() with open(os.environ["FAKE_BACKEND_IMPOSTOR"], "w") as f: f.write(str(os.getpid())) ThreadingHTTPServer(("127.0.0.1", port), H).serve_forever() diff --git a/tests/smoke_serve.sh b/tests/smoke_serve.sh index 208f06aaf..6d1f50f4a 100755 --- a/tests/smoke_serve.sh +++ b/tests/smoke_serve.sh @@ -120,6 +120,13 @@ if [ "$(uname)" = Linux ]; then imp_rc=$? check "a port held by another process is refused (rc $imp_rc)" \ '[ "$imp_rc" = 1 ] && grep -q "not from the backend serve started" "$scratch/impostor.log"' + # The impostor detaches itself (setsid), so killing the backend does not reap it and the port stays LISTENing + # for the rest of the suite. Measured before the fix: 1 listener survived; the fake backend now dies with it. + if [ -f "$scratch/impostor" ]; then + imp=$(cat "$scratch/impostor") + for _ in 1 2 3 4 5 6 7 8 9 10; do kill -0 "$imp" 2>/dev/null || break; sleep 0.2; done + check "the impostor does not outlive the case (pid $imp)" '! kill -0 "$imp" 2>/dev/null' + fi fi if [ $fail -ne 0 ]; then echo "--- serve log"; cat "$scratch/serve.log"; echo FAIL; exit 1; fi