Skip to content

The OSINT investigator's workbench. One case, one folder, every tool. Local-first: your media and your investigations never leave your machine.

JavaScript

Stop the whole binary on Windows so the smoke test can clean up

OsintMeThat7f638218d806
diff --git a/scripts/smoke_binary.py b/scripts/smoke_binary.py
index ec51067..9b5168e 100644
--- a/scripts/smoke_binary.py
+++ b/scripts/smoke_binary.py
@@ -11,6 +11,7 @@ import json
import os
import socket
import subprocess
+import sys
import tempfile
import time
import urllib.error
@@ -65,9 +66,27 @@ def wait_until_ready(base_url: str, process: subprocess.Popen[bytes], timeout: f
def stop(process: subprocess.Popen[bytes]) -> None:
+ """Stop the binary, children included.
+
+ PyInstaller builds this artifact in onefile mode, so the `.exe` is a
+ bootloader that unpacks itself and runs the application as a *child*. On
+ POSIX the bootloader forwards the signal down; on Windows there is nothing
+ to forward it — `terminate()` is `TerminateProcess` on the bootloader alone,
+ and the application keeps running with the workspace lock file still open.
+ Windows cannot delete an open file, so the temporary workspace below refuses
+ to clean up and the release fails after the binary already passed.
+ """
if process.poll() is not None:
return
- process.terminate()
+ if sys.platform == "win32":
+ # taskkill walks the tree; /F because the orphan has no console to ask.
+ subprocess.run(
+ ["taskkill", "/F", "/T", "/PID", str(process.pid)],
+ capture_output=True,
+ check=False,
+ )
+ else:
+ process.terminate()
try:
process.wait(timeout=10)
except subprocess.TimeoutExpired:
diff --git a/tests/test_binary_smoke.py b/tests/test_binary_smoke.py
index 8dcc477..531fcc6 100644
--- a/tests/test_binary_smoke.py
+++ b/tests/test_binary_smoke.py
@@ -55,3 +55,70 @@ def test_check_application_rejects_path_ffmpeg(monkeypatch):
with pytest.raises(RuntimeError, match="bundled ffmpeg"):
smoke_binary.check_application("http://127.0.0.1:8477")
+
+
+class FakeProcess:
+ """Enough of Popen for stop(): it reports alive, then dies when asked."""
+
+ def __init__(self, alive: bool = True):
+ self.pid = 4242
+ self._alive = alive
+ self.terminated = False
+
+ def poll(self):
+ return None if self._alive else 0
+
+ def terminate(self):
+ self.terminated = True
+ self._alive = False
+
+ def kill(self): # pragma: no cover - only a hung process reaches this
+ self._alive = False
+
+ def wait(self, timeout=None):
+ self._alive = False
+ return 0
+
+
+def test_stop_kills_the_whole_tree_on_windows(monkeypatch):
+ """The onefile bootloader's child holds the workspace lock open.
+
+ Terminating the bootloader alone leaves it running, and Windows then refuses
+ to delete the temporary workspace it is writing in.
+ """
+ monkeypatch.setattr(smoke_binary.sys, "platform", "win32")
+ calls = []
+ monkeypatch.setattr(
+ smoke_binary.subprocess, "run", lambda cmd, **kw: calls.append(cmd)
+ )
+ process = FakeProcess()
+
+ smoke_binary.stop(process)
+
+ assert calls == [["taskkill", "/F", "/T", "/PID", "4242"]]
+ assert not process.terminated
+
+
+def test_stop_signals_the_bootloader_on_posix(monkeypatch):
+ monkeypatch.setattr(smoke_binary.sys, "platform", "linux")
+ monkeypatch.setattr(
+ smoke_binary.subprocess,
+ "run",
+ lambda *a, **kw: pytest.fail("taskkill is Windows-only"),
+ )
+ process = FakeProcess()
+
+ smoke_binary.stop(process)
+
+ assert process.terminated
+
+
+def test_stop_leaves_an_already_exited_process_alone(monkeypatch):
+ monkeypatch.setattr(smoke_binary.sys, "platform", "win32")
+ monkeypatch.setattr(
+ smoke_binary.subprocess,
+ "run",
+ lambda *a, **kw: pytest.fail("nothing left to kill"),
+ )
+
+ smoke_binary.stop(FakeProcess(alive=False))