fix(gateway): handle Windows stop fallback
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import json
|
||||
import signal
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
from nanobot.gateway import GatewayRuntime, GatewayRuntimePaths, GatewayStartOptions
|
||||
@@ -168,3 +170,42 @@ def test_stop_keeps_state_when_process_survives_timeout(tmp_path, monkeypatch):
|
||||
assert result.status.running is True
|
||||
assert result.status.reason == "stop_timeout"
|
||||
assert runtime.paths.state_path.exists()
|
||||
|
||||
|
||||
def test_terminate_windows_falls_back_when_ctrl_break_is_rejected(tmp_path, monkeypatch):
|
||||
taskkill_calls: list[dict] = []
|
||||
|
||||
def fake_run(command, **kwargs):
|
||||
taskkill_calls.append({"command": command, "kwargs": kwargs})
|
||||
|
||||
runtime = GatewayRuntime(
|
||||
paths=_paths(tmp_path),
|
||||
platform_name="Windows",
|
||||
subprocess_run=fake_run,
|
||||
sleep=lambda _seconds: None,
|
||||
)
|
||||
|
||||
monkeypatch.setattr(signal, "CTRL_BREAK_EVENT", 1, raising=False)
|
||||
|
||||
def fake_kill(_pid, _signal):
|
||||
raise OSError(87, "The parameter is incorrect")
|
||||
|
||||
monkeypatch.setattr("nanobot.gateway.runtime.os.kill", fake_kill)
|
||||
|
||||
def fake_wait_for_exit(_pid, _timeout_s):
|
||||
# Simulate a process that only exits after the taskkill fallback runs.
|
||||
return bool(taskkill_calls)
|
||||
|
||||
monkeypatch.setattr(runtime, "_wait_for_exit", fake_wait_for_exit)
|
||||
|
||||
assert runtime._terminate_windows(12345, timeout_s=20) is True
|
||||
assert taskkill_calls == [
|
||||
{
|
||||
"command": ["taskkill", "/PID", "12345", "/T"],
|
||||
"kwargs": {
|
||||
"check": False,
|
||||
"stdout": subprocess.DEVNULL,
|
||||
"stderr": subprocess.DEVNULL,
|
||||
},
|
||||
}
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user