Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions tests/fake_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions tests/smoke_serve.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading