fix(runtime): keep the Windows kill round distinct from terminate (#3177)

* fix(runtime): keep the Windows kill round distinct from terminate

* test(runtime): let dead-owner recovery settle on Windows

A just-exited PID stays visible to psutil for a few milliseconds on Windows, so one immediate recovery tick can defer without being wrong. Poll with a bounded deadline instead of asserting the very first tick.

---------

Co-authored-by: Bingxi Zhao (Frank) <13051345080@163.com>
This commit is contained in:
qiuqianyi66
2026-12-02 18:47:06 +08:00
committed by GitHub
parent faca029cb5
commit a021cfea0e
4 changed files with 31 additions and 7 deletions

View File

@@ -62,7 +62,12 @@ def _ready_timeout(env_name: str, default: int) -> int:
BACKEND_READY_TIMEOUT = _ready_timeout(BACKEND_READY_TIMEOUT_ENV, 60)
FRONTEND_READY_TIMEOUT = _ready_timeout(FRONTEND_READY_TIMEOUT_ENV, 120)
FRONTEND_REUSE_PROBE_TIMEOUT = 2
KILL_SIGNAL = getattr(signal, "SIGKILL", signal.SIGTERM)
# Windows has no SIGKILL. Falling back to SIGTERM there would make the
# graceful round indistinguishable from the forced one: every taskkill would
# carry /F, and _kill_port_listeners would escalate from SIGTERM to SIGTERM.
# SIGBREAK exists only on Windows and is never delivered directly — the
# taskkill branch reads it as "append /F".
KILL_SIGNAL = signal.SIGKILL if hasattr(signal, "SIGKILL") else signal.SIGBREAK
SHUTDOWN_GRACE_SECONDS = 3.0
SHUTDOWN_REAP_SECONDS = 1.0
WEB_CACHE_DIR = Path("data") / "user" / "runtime" / "web"

View File

@@ -256,7 +256,15 @@ asyncio.run(run())
assert await store.get_events("cli-turn") == original
process.terminate()
await asyncio.to_thread(process.wait, 5)
assert await recovery.recover_once() == 1
# Windows keeps a just-exited PID visible to psutil for a few
# milliseconds, so a single immediate tick can legitimately defer.
recovered = 0
for _ in range(100):
recovered = await recovery.recover_once()
if recovered:
break
await asyncio.sleep(0.02)
assert recovered == 1
assert (await store.get_turn("cli-turn"))["failure_code"] == "worker_lost"
assert [event["seq"] for event in await store.get_events("cli-turn")] == [1, 2, 3]
assert await recovery.recover_once() == 0

View File

@@ -1300,7 +1300,7 @@ def test_terminate_warns_when_both_signal_rounds_fail(
assert sent == [signal.SIGTERM, launcher.KILL_SIGNAL]
warnings = [record.getMessage() for record in caplog.records]
assert any("SIGTERM" in message for message in warnings)
assert any("SIGKILL" in message for message in warnings)
assert any(launcher.KILL_SIGNAL.name in message for message in warnings)
def test_terminate_warns_on_sigterm_but_accepts_kill_escalation(
@@ -1326,7 +1326,7 @@ def test_terminate_warns_on_sigterm_but_accepts_kill_escalation(
assert child.exited is True
warnings = [record.getMessage() for record in caplog.records]
assert any("SIGTERM" in message for message in warnings)
assert not any("SIGKILL" in message for message in warnings)
assert not any(launcher.KILL_SIGNAL.name in message for message in warnings)
def test_terminate_skips_absent_or_exited_child(
@@ -1442,7 +1442,7 @@ def test_kill_port_listeners_reports_kill_round_failures_per_target(
assert (456, signal.SIGTERM) in sent
assert (123, launcher.KILL_SIGNAL) in sent
assert (456, launcher.KILL_SIGNAL) in sent
assert any("SIGKILL" in record.getMessage() for record in caplog.records)
assert any(launcher.KILL_SIGNAL.name in record.getMessage() for record in caplog.records)
@pytest.mark.skipif(sys.platform != "win32", reason="Windows venv console inheritance")

View File

@@ -92,7 +92,9 @@ def test_send_tree_signal_uses_process_group_when_known(monkeypatch) -> None:
killpg_calls: list[tuple[int, signal.Signals]] = []
kill_calls: list[tuple[int, signal.Signals]] = []
monkeypatch.setattr(launcher.os, "name", "posix")
monkeypatch.setattr(launcher.os, "killpg", lambda pgid, sig: killpg_calls.append((pgid, sig)))
monkeypatch.setattr(
launcher.os, "killpg", lambda pgid, sig: killpg_calls.append((pgid, sig)), raising=False
)
monkeypatch.setattr(launcher.os, "kill", lambda pid, sig: kill_calls.append((pid, sig)))
launcher._send_tree_signal(4242, 4242, signal.SIGTERM)
@@ -105,7 +107,9 @@ def test_send_tree_signal_falls_back_to_single_pid_without_pgid(monkeypatch) ->
killpg_calls: list[tuple[int, signal.Signals]] = []
kill_calls: list[tuple[int, signal.Signals]] = []
monkeypatch.setattr(launcher.os, "name", "posix")
monkeypatch.setattr(launcher.os, "killpg", lambda pgid, sig: killpg_calls.append((pgid, sig)))
monkeypatch.setattr(
launcher.os, "killpg", lambda pgid, sig: killpg_calls.append((pgid, sig)), raising=False
)
monkeypatch.setattr(launcher.os, "kill", lambda pid, sig: kill_calls.append((pid, sig)))
launcher._send_tree_signal(4242, None, signal.SIGTERM)
@@ -124,6 +128,13 @@ def test_send_tree_signal_ignores_missing_pid(monkeypatch) -> None:
assert kill_calls == []
def test_the_kill_signal_cannot_be_confused_with_terminate() -> None:
"""The ladder sends TERM, waits, then sends KILL. Windows has no SIGKILL;
if the fallback were SIGTERM, the graceful taskkill and the forced one
would be the same command and the ladder would have one real round."""
assert launcher.KILL_SIGNAL != signal.SIGTERM
def test_send_tree_signal_windows_uses_taskkill_with_force_only_on_kill(
monkeypatch,
) -> None: