From 20d2c655b4f550cd18cad074a4f62a7ac330c6ac Mon Sep 17 00:00:00 2001 From: mark-yong Date: Sun, 29 Nov 2026 01:56:26 +0800 Subject: [PATCH] fix: keep default Windows Ctrl-C termination while loading `flare run` on Windows previously registered `stop_console` via `SetConsoleCtrlHandler` in `run()` (`flare-cli/src/main.rs`) before core download, trust verification, and GPU model loading. Because `stop_console` returns `1` ("handled") for `CTRL_C_EVENT` and `CTRL_BREAK_EVENT`, it suppressed Windows's default process termination while nothing checked `STOP` until the serving loop (`distribution` and `launch` are `#[cfg(unix)]`, and Windows rejects multi-host flags). A Ctrl-C during download or loading was therefore swallowed, leaving forced termination as the only exit path. Installing the handler after `loaded.start()` in `serve_local()` rather than earlier avoids keeping Ctrl-C suppressed in two places that never poll `STOP`: Windows rejects multi-host flags before `serve()` and routes local runs into `serve_local()`, while `loaded.start()` invokes `Server::start` and runs startup tuning graphs (`helios/src/execution/transport.rs`) under a 120-second completion deadline before the serving loop starts. Returning an error if `SetConsoleCtrlHandler` fails after `loaded.start()` remains safe because `LoadedSession::start()` wraps the model owner in `DrainGuard` and `ServingSession::drop()` calls `close()` (`helios/src/server/session.rs`), draining or retaining GPU storage rather than releasing an undrained owner. Removed the early `#[cfg(windows)]` `SetConsoleCtrlHandler(Some(stop_console), 1)` block from `run()` in `flare-cli/src/main.rs` and added it in `serve_local()` immediately after `loaded.start()` alongside the `#[cfg(unix)]` signal block, retaining the `== 0` OS-error check. Left `serve()` unchanged and rewrote the Ctrl-C sentence in `docs/windows.md` in place to state that Ctrl-C and Ctrl-Break end loading immediately and drain native work before releasing CUDA resources once serving has started. On Windows, Ctrl-C and Ctrl-Break now preserve default immediate termination throughout download, verification, loading, and server startup, and drain native replay before releasing CUDA resources once serving begins. Unix behavior is unchanged. On a Linux x86_64 host against this commit's tree, `cargo test --locked --release --manifest-path flare-cli/Cargo.toml` (with `CAP_DAC_OVERRIDE` dropped via `setpriv` so read-only cache permission tests enforce `EACCES`) passed (`exit 0`) across all five test binaries: `src/main.rs` unit tests (201 passed, 0 failed, 3 ignored for multi-host hardware/payload fixtures), `tests/cache_commands.rs` (9 passed), `tests/cli.rs` (6 passed), `tests/inspect.rs` (4 passed), and `tests/unverified_payload.rs` (7 passed), totaling 227 passed, 0 failed, and 3 ignored. Cross-compilation checking from Linux via `cargo xwin check --locked --release --manifest-path flare-cli/Cargo.toml --target x86_64-pc-windows-msvc` passed (`exit 0`), verifying compilation of `flare-cli` including the `#[cfg(windows)]` `SetConsoleCtrlHandler` path. Full cross-linking via `cargo xwin build --locked --release` failed at `lld-link` (`exit 101`) due to pre-existing cross-build mismatches unrelated to `flare-cli/src/main.rs`: default CRT linking hit a `RuntimeLibrary` mismatch between `libflare_selene` (`MD_DynamicRelease`) and `libesaxx_rs` (`MT_StaticRelease`), while `RUSTFLAGS="-C target-feature=+crt-static"` hit undefined `__cpu_model` and `__cpu_features2` symbols referenced by `libflare_selene`. Limitations: Windows executable linking, Windows native test execution, and live Windows `flare run CORE` Ctrl-C verification (immediate termination during loading and clean drain after `"Start server"`) were not run. Refs: #113 Signed-off-by: Mark Yong Design-Signed-Off-By: Antigravity (Gemini 4 Neon); design-review=2e5b --- docs/windows.md | 4 ++-- flare-cli/src/main.rs | 13 +++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/docs/windows.md b/docs/windows.md index 8b565115c..e1b0370ff 100644 --- a/docs/windows.md +++ b/docs/windows.md @@ -138,8 +138,8 @@ configured by `scripts/dev_windows.ps1` builds Publisher acceptance, request sampling, graph-capacity selection, and HTTP APIs follow the [CLI specification](cli-design.md). The default trust store is `Flare\trust.toml` under the Windows Local App Data known folder. `--trust-store` -selects another path. Ctrl-C and Ctrl-Break drain native work before releasing -CUDA resources. +selects another path. Ctrl-C and Ctrl-Break end loading immediately and drain +native work before releasing CUDA resources once serving has started. Disk embedding tables use Windows I/O rings with registered files and buffers. All core shards share each worker's ring and staging allocation; file indices diff --git a/flare-cli/src/main.rs b/flare-cli/src/main.rs index c66489c99..08925c46f 100644 --- a/flare-cli/src/main.rs +++ b/flare-cli/src/main.rs @@ -184,12 +184,6 @@ fn run() -> Result> { { return Err("multi-host serving is not supported on Windows".into()); } - #[cfg(windows)] - unsafe { - if windows_sys::Win32::System::Console::SetConsoleCtrlHandler(Some(stop_console), 1) == 0 { - return Err(std::io::Error::last_os_error().into()); - } - } let bind = args.bind_address(); if args.rank.is_some_and(|rank| rank != 0) { args.serving.select_gpus(1); @@ -707,6 +701,13 @@ fn serve_local( libc::signal(libc::SIGINT, stop as *const () as libc::sighandler_t); libc::signal(libc::SIGTERM, stop as *const () as libc::sighandler_t); } + #[cfg(windows)] + unsafe { + // Loading keeps the default Ctrl-C termination; nothing reads STOP before serving. + if windows_sys::Win32::System::Console::SetConsoleCtrlHandler(Some(stop_console), 1) == 0 { + return Err(std::io::Error::last_os_error().into()); + } + } progress.finish_animation(); if let Err(error) = server.start_dashboard() { eprintln!("Could not start dashboard: {error}");