mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-08 13:12:08 +00:00
fix(file-sync): re-deliver deferred Ctrl+C via raise_signal, not os.kill (Windows hard-kill)
_sync_back_once defers a SIGINT that lands mid-sync, then re-delivers it once the sync completes so the user's Ctrl+C isn't lost. It did so with os.kill(os.getpid(), signal.SIGINT). That is not graceful on Windows: os.kill only treats CTRL_C_EVENT(0)/CTRL_BREAK_EVENT(1) as console events; any other value (SIGINT == 2) routes to TerminateProcess(sig), so a Ctrl+C during a remote-backend (ssh/daytona/modal) sync-back hard-kills the whole CLI session (exit code 2) on Windows instead of raising KeyboardInterrupt. Use signal.raise_signal(signal.SIGINT) (3.8+), which invokes the restored handler through C raise() on every platform. Verified on Windows: raise_signal runs the handler (graceful) while os.kill(getpid, SIGINT) TerminateProcess-es the process. Adds a cross-platform regression test that runs on Windows too (it stubs the locked sync body, so unlike test_file_sync_back.py it needs no fcntl).
This commit is contained in:
parent
c67aab763d
commit
a88e0fd2ab
2 changed files with 67 additions and 1 deletions
56
tests/tools/test_file_sync_sigint.py
Normal file
56
tests/tools/test_file_sync_sigint.py
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
"""Cross-platform regression for the deferred-SIGINT re-delivery in sync-back.
|
||||
|
||||
``_sync_back_once`` defers a Ctrl+C that lands mid-sync, then re-delivers it once
|
||||
the sync completes. It must do so via ``signal.raise_signal`` — which invokes the
|
||||
handler through C ``raise()`` on every platform — and NOT via
|
||||
``os.kill(os.getpid(), signal.SIGINT)``: on Windows the latter routes SIGINT (2)
|
||||
to ``TerminateProcess`` and hard-kills the whole CLI instead of raising
|
||||
``KeyboardInterrupt``.
|
||||
|
||||
Unlike ``test_file_sync_back.py`` this module does not depend on ``fcntl`` (the
|
||||
locked sync body is stubbed), so it runs on Windows too — the platform the bug
|
||||
actually manifests on.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import signal
|
||||
|
||||
from tools.environments.file_sync import FileSyncManager
|
||||
|
||||
|
||||
def _make_manager() -> FileSyncManager:
|
||||
return FileSyncManager(
|
||||
get_files_fn=lambda: {},
|
||||
upload_fn=lambda *a, **k: None,
|
||||
delete_fn=lambda *a, **k: None,
|
||||
)
|
||||
|
||||
|
||||
def test_deferred_sigint_redelivered_via_raise_signal(tmp_path, monkeypatch):
|
||||
mgr = _make_manager()
|
||||
|
||||
# Simulate a Ctrl+C arriving during the sync body: invoke the deferring
|
||||
# handler that _sync_back_once installed, so `deferred_sigint` is populated.
|
||||
def fake_locked(lock_path):
|
||||
signal.getsignal(signal.SIGINT)(signal.SIGINT, None)
|
||||
|
||||
monkeypatch.setattr(mgr, "_sync_back_locked", fake_locked)
|
||||
|
||||
raised: list[int] = []
|
||||
killed: list[tuple[int, int]] = []
|
||||
monkeypatch.setattr(
|
||||
"tools.environments.file_sync.signal.raise_signal", raised.append
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"tools.environments.file_sync.os.kill",
|
||||
lambda pid, sig: killed.append((pid, sig)),
|
||||
)
|
||||
|
||||
mgr._sync_back_once(tmp_path / "sync.lock")
|
||||
|
||||
# The deferred Ctrl+C is re-delivered cross-platform via raise_signal,
|
||||
assert raised == [signal.SIGINT]
|
||||
# and never through os.kill(getpid, SIGINT) (which hard-kills on Windows).
|
||||
assert (os.getpid(), signal.SIGINT) not in killed
|
||||
|
|
@ -302,7 +302,17 @@ class FileSyncManager:
|
|||
if on_main_thread and original_handler is not None:
|
||||
signal.signal(signal.SIGINT, original_handler)
|
||||
if deferred_sigint:
|
||||
os.kill(os.getpid(), signal.SIGINT)
|
||||
# Re-deliver the deferred Ctrl+C to the just-restored
|
||||
# handler. ``os.kill(os.getpid(), signal.SIGINT)`` is NOT a
|
||||
# graceful signal on Windows: os.kill only treats
|
||||
# CTRL_C_EVENT(0)/CTRL_BREAK_EVENT(1) as console events; any
|
||||
# other value (SIGINT == 2) routes to TerminateProcess(sig),
|
||||
# hard-killing the CLI (exit code 2) instead of raising
|
||||
# KeyboardInterrupt — so a Ctrl+C during a remote-backend
|
||||
# sync-back would kill the whole session on Windows.
|
||||
# ``signal.raise_signal`` (3.8+) invokes the handler via C
|
||||
# ``raise()`` on every platform.
|
||||
signal.raise_signal(signal.SIGINT)
|
||||
|
||||
def _sync_back_locked(self, lock_path: Path) -> None:
|
||||
"""Sync-back under file lock (serializes concurrent gateways)."""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue