Commit 48ac73696 for llama.cpp
commit 48ac7369600b7b760c91bd87d1527cadda9df517
Author: Baran Bati <66915560+Batibaran@users.noreply.github.com>
Date: Wed Nov 25 14:29:01 2026 +0300
server : handle CTRL_BREAK_EVENT on Windows for clean shutdown (#32080)
Windows can't send SIGINT to a child process, so scripts that launch
llama-server could only kill it, which skips the clean shutdown. The
console control handler only handled CTRL_C_EVENT, so a CTRL_BREAK_EVENT
sent to the server's own process group ended it with
STATUS_CONTROL_C_EXIT.
- server: handle CTRL_BREAK_EVENT the same as CTRL_C_EVENT in the
console control handler
- scripts/moe-cache-tune.py: on Windows, start the server in a new
process group and stop it with CTRL_BREAK_EVENT. On other platforms,
stop it with SIGINT. In both cases, kill it afterwards as a fallback.
- tests: add test_server_ctrl_break (Windows only) and a
ServerProcess.share_console option. Console control events only
reach a server that shares the test's console, hence the option.
diff --git a/scripts/moe-cache-tune.py b/scripts/moe-cache-tune.py
index d0680fa74..e41ed266e 100755
--- a/scripts/moe-cache-tune.py
+++ b/scripts/moe-cache-tune.py
@@ -26,6 +26,9 @@ SHARES = [0.0, 0.5, 0.8, 0.9, 0.95, 0.98]
CALIB_SHARES = [0.0, 0.5, 0.95]
N_FIRST = 2048 # tokens per request, first run
N_CALIB = 512 # tokens per request, other runs
+# Windows cannot send SIGINT to a child, llama-server stops cleanly on CTRL_BREAK_EVENT sent to its own process group
+STOP_SIGNAL = signal.CTRL_BREAK_EVENT if sys.platform == "win32" else signal.SIGINT
+NEW_GROUP = subprocess.CREATE_NEW_PROCESS_GROUP if sys.platform == "win32" else 0
PROMPT = ("The lighthouse keeper had kept a logbook for thirty-one years, and in all that time he had never once written down a lie. "
"So when the ship appeared on the horizon on the first calm morning of October, with no flag, no lights and no one on deck, "
"he sat at his small wooden table for a long while before he picked up the pen. Below him the sea was flat and grey, and the "
@@ -502,7 +505,8 @@ def run_server(cmd, out, mib, share, n_predict, seed, label, warm):
timings: list = []
for attempt in range(2):
with open(base + ".log", "w") as log:
- proc = subprocess.Popen(args, stdout=log, stderr=subprocess.STDOUT, env=dict(os.environ, LLAMA_MOE_CACHE_TRACE=base + ".trace"))
+ proc = subprocess.Popen(args, stdout=log, stderr=subprocess.STDOUT, env=dict(os.environ, LLAMA_MOE_CACHE_TRACE=base + ".trace"),
+ creationflags=NEW_GROUP)
try:
while True:
if proc.poll() is not None:
@@ -517,11 +521,13 @@ def run_server(cmd, out, mib, share, n_predict, seed, label, warm):
timings = [http(port, "/completion", {"prompt": PROMPT, "n_predict": n, "ignore_eos": True, "cache_prompt": False,
"temperature": 0.8, "top_p": 0.95, "seed": seed + k})["timings"] for k in (0, 1)]
finally:
- proc.send_signal(signal.SIGINT)
try:
+ proc.send_signal(STOP_SIGNAL)
proc.wait(timeout=120)
except subprocess.TimeoutExpired:
- proc.kill()
+ pass
+ finally:
+ proc.kill() # no-op once it exited, else the clean stop failed
# the second generation has a warmer cache, it is much slower only if the machine is busy
if timings[1]["predicted_per_second"] > timings[0]["predicted_per_second"] / 1.5 or attempt == 1:
break
diff --git a/tools/server/server.cpp b/tools/server/server.cpp
index e022355dc..8b9b73db7 100644
--- a/tools/server/server.cpp
+++ b/tools/server/server.cpp
@@ -344,7 +344,7 @@ static void server_install_signal_handlers() {
sigaction(SIGTERM, &sigint_action, NULL);
#elif defined (_WIN32)
auto console_ctrl_handler = +[](DWORD ctrl_type) -> BOOL {
- return (ctrl_type == CTRL_C_EVENT) ? (signal_handler(SIGINT), true) : false;
+ return (ctrl_type == CTRL_C_EVENT || ctrl_type == CTRL_BREAK_EVENT) ? (signal_handler(SIGINT), true) : false;
};
SetConsoleCtrlHandler(reinterpret_cast<PHANDLER_ROUTINE>(console_ctrl_handler), true);
#endif
diff --git a/tools/server/tests/unit/test_basic.py b/tools/server/tests/unit/test_basic.py
index ad3cea679..8550ec78c 100644
--- a/tools/server/tests/unit/test_basic.py
+++ b/tools/server/tests/unit/test_basic.py
@@ -1,6 +1,8 @@
import pytest
import requests
+import signal
import socket
+import sys
from utils import *
server = ServerPreset.tinyllama2()
@@ -159,3 +161,13 @@ def test_server_models_sse_single_model():
assert event["event"] == "status_change"
assert event["data"]["status"] == "loaded"
assert event["data"]["info"]["id"] == model_id
+
+
+@pytest.mark.skipif(sys.platform != "win32", reason="Windows console control event")
+def test_server_ctrl_break():
+ global server
+ server.share_console = True
+ server.start()
+ server.process.send_signal(signal.CTRL_BREAK_EVENT)
+ # exit code 0 means a clean shutdown, the default handler would end it with STATUS_CONTROL_C_EXIT
+ assert server.process.wait(timeout=60) == 0
diff --git a/tools/server/tests/utils.py b/tools/server/tests/utils.py
index 6dbd540b9..a9f9fba2e 100644
--- a/tools/server/tests/utils.py
+++ b/tools/server/tests/utils.py
@@ -141,6 +141,7 @@ class ServerProcess:
mcp_servers_json: str | None = None
cors_origins: str | None = None
llama_cache: str | None = None
+ share_console: bool = False # Windows: console control events only reach a server on the console of the tests
# session variables
process: subprocess.Popen | None = None
@@ -340,9 +341,10 @@ class ServerProcess:
flags = 0
if "nt" == os.name:
- flags |= subprocess.DETACHED_PROCESS
flags |= subprocess.CREATE_NEW_PROCESS_GROUP
- flags |= subprocess.CREATE_NO_WINDOW
+ if not self.share_console:
+ flags |= subprocess.DETACHED_PROCESS
+ flags |= subprocess.CREATE_NO_WINDOW
if self.log_path:
self._log = open(self.log_path, "w")