The agent that turns websites into APIs!
fix(windows): stop the managed OpenCode process tree
kalil032106fb95fe4906
diff --git a/src/reverse_api/opencode_runtime.py b/src/reverse_api/opencode_runtime.pyindex 6f6333a..400fb9a 100644--- a/src/reverse_api/opencode_runtime.py+++ b/src/reverse_api/opencode_runtime.py@@ -12,6 +12,7 @@ import osimport reimport shutilimport subprocess+import sysimport threadingimport timefrom collections.abc import Mapping@@ -381,6 +382,21 @@ def stop_managed_opencode_server() -> None:if process is None or process.poll() is not None:return+ if sys.platform == "win32":+ # npx.cmd -> node -> opencode: terminating only the launcher leaves+ # the server listening. Kill its tree while the parent PID still exists.+ taskkill = os.path.join(os.environ.get("SystemRoot", r"C:\Windows"), "System32", "taskkill.exe")+ try:+ result = subprocess.run(+ [taskkill, "/PID", str(process.pid), "/T", "/F"],+ stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,+ timeout=5, check=False,+ )+ if result.returncode == 0:+ process.wait(timeout=3)+ return+ except (OSError, subprocess.TimeoutExpired):+ pass # Best-effort parent cleanup if taskkill is unavailable.try:process.terminate()except OSError:diff --git a/tests/test_opencode_runtime.py b/tests/test_opencode_runtime.pyindex cfbe5af..1e76a84 100644--- a/tests/test_opencode_runtime.py+++ b/tests/test_opencode_runtime.py@@ -15,7 +15,9 @@ def reset_runtime_state():opencode_runtime._PROCESS = Noneopencode_runtime._PROCESS_URL = Noneopencode_runtime._PROCESS_ENV_OVERRIDES = {}- yield+ # Unit tests use fake Popen objects; never run native taskkill on their PIDs.+ with patch.object(opencode_runtime.subprocess, "run", return_value=MagicMock(returncode=1)):+ yieldopencode_runtime._PROCESS = Noneopencode_runtime._PROCESS_URL = Noneopencode_runtime._PROCESS_ENV_OVERRIDES = {}@@ -278,3 +280,28 @@ def test_stop_managed_server_terminates_child():process.terminate.assert_called_once_with()process.wait.assert_called_once_with(timeout=3)assert opencode_runtime._PROCESS is None+++def test_windows_shutdown_terminates_entire_tree():+ process = MagicMock(pid=12345)+ process.poll.return_value = None+ opencode_runtime._PROCESS = process+ with patch.object(opencode_runtime.sys, "platform", "win32"):+ with patch.object(opencode_runtime.subprocess, "run", return_value=MagicMock(returncode=0)) as run:+ opencode_runtime.stop_managed_opencode_server()+ assert run.call_args.args[0][1:] == ["/PID", "12345", "/T", "/F"]+ assert run.call_args.args[0][0].endswith("taskkill.exe")+ process.terminate.assert_not_called()+ process.wait.assert_called_once_with(timeout=3)+ assert opencode_runtime._PROCESS is None+++def test_windows_shutdown_falls_back_when_taskkill_unavailable():+ process = MagicMock(pid=12345)+ process.poll.return_value = None+ opencode_runtime._PROCESS = process+ with patch.object(opencode_runtime.sys, "platform", "win32"):+ with patch.object(opencode_runtime.subprocess, "run", side_effect=OSError("missing taskkill")):+ opencode_runtime.stop_managed_opencode_server()+ process.terminate.assert_called_once_with()+ process.wait.assert_called_once_with(timeout=3)