Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

serve: honour a failing before_load hook (503 + Retry-After), configurable timeout, /unload?wait=1 (#1997)

Niko1221c7838c529f02
diff --git a/docs/DETAILS.md b/docs/DETAILS.md
index 4ae2710b8..bcdad0313 100644
--- a/docs/DETAILS.md
+++ b/docs/DETAILS.md
@@ -563,8 +563,9 @@ server, three server options (all off by default; also as keys in `strata-<model
| `--idle-unload 600` | `"idle_unload_s": 600` | unload the model after 600 s without requests; the next request loads it again |
| `--min-free-vram-mib 11000` | `"min_free_vram_mib": 11000` | load an unloaded model only when that much VRAM is free (it waits up to 15 s for memory being given back), else answer **503** "the GPU is in use by another program" instead of starting into what a game left (with several GPUs it checks the first one) |
| `--before-load "cmd"` | `"before_load": "cmd"` or `["cmd", "arg"]` | a command run before the model is loaded again, e.g. one that unloads another server's model |
+| | `"before_load_timeout_s": 120` | how long that command may run (default 120 s). When it exits non-zero or times out (a peer that is not running makes `curl` exit 7), the free VRAM decides: with `min_free_vram_mib` set and not enough free, the model is **not** loaded and the request gets **503** with `Retry-After`; with room (or no limit set) it loads, with a warning in the log. `"before_load_strict": true` always refuses on a failure; `"before_load_ignore_failure": true` always loads |
-`POST /unload` unloads it now (`409` while a request is running) and `POST /load` loads it ahead of a request (both
+`POST /unload` unloads it now (`409` while a request is running; `POST /unload?wait=1` waits for the running turn first, up to 300 s, or `?wait=<seconds>`, so a second server can really free the card) and `POST /load` loads it ahead of a request (both
with `Content-Type: application/json`, e.g. `curl -X POST -H "Content-Type: application/json" localhost:8080/unload`);
`/health` says `"loaded"`, `/v1/models` lists it as `unloaded` (like llama.cpp's router), `/props` sets
`is_sleeping` and the Monitor shows the state. Unloading ends the engine process - and the image encoder, when images
diff --git a/serve/server.py b/serve/server.py
index 1183a66d4..8e61c180c 100644
--- a/serve/server.py
+++ b/serve/server.py
@@ -346,7 +346,28 @@ class EngineStuck(RuntimeError):
class GpuBusy(RuntimeError):
"""The model is unloaded and the GPU has less free VRAM than min_free_vram_mib: something else (a game, another
- model server) is using it, so the engine is not started into the little that is left."""
+ model server) is using it, so the engine is not started into the little that is left. `retry_after`: seconds
+ for the Retry-After header of the 503."""
+
+ def __init__(self, message="", retry_after=None):
+ super().__init__(message)
+ self.retry_after = retry_after
+
+
+def unload_wait(value, default: float) -> float:
+ """#1997: the `wait` of POST /unload?wait=...: absent / 0 / false: 0 (answer "busy" at once, as before); 1 / true /
+ yes: `default` seconds; a number of seconds above 1: that long (at most an hour)."""
+ if value is None:
+ return 0.0
+ v = str(value).strip().lower()
+ if v in ("", "0", "false", "no", "off"):
+ return 0.0
+ if v in ("1", "true", "yes", "on"):
+ return default
+ try:
+ return min(3600.0, max(0.0, float(v)))
+ except ValueError:
+ return default
ENGINE_REQUEST = re.compile(
@@ -1476,7 +1497,8 @@ class StrataEngine:
print(f"[strata] parallel: the engine refused slot {slot} ({e}); trying another slot "
f"({refused}/{self.BGEN_REFUSALS_MAX}) (issue #1994)", flush=True)
if refused > self.BGEN_REFUSALS_MAX:
- raise GpuBusy("the engine's batch slots are not free right now; try again in a moment") from e
+ raise GpuBusy("the engine's batch slots are not free right now; try again in a moment",
+ retry_after=5) from e
self.slot_live[slot] = None
self.slot_held[slot] = []
phase = "none"
@@ -2892,6 +2914,10 @@ class Service:
self.idle_unload_s = 0
self.min_free_vram_mib = 0
self.before_load = None
+ self.before_load_timeout_s = 120.0 # #1997: how long the before_load command may run
+ self.before_load_ignore_failure = False # #1997: load even when it fails, without a VRAM check
+ self.vram_free_wait_s = 15.0 # how long a full card is waited on (memory given back)
+ self.before_load_strict = False # #1997: refuse (503) whenever it fails
self.vram_reserve = None # #533: the last POST /v1/vram reserve (None: the start's)
self.reasoning_loop_recovery = False
self.reasoning_budget_tokens = 0 # #123: the config's default thinking budget (0: none)
@@ -3041,22 +3067,55 @@ class Service:
the free-VRAM check. The caller holds self.fifo."""
if self.loaded() and not self._vision_down():
return
+ hook_failed = None
if self.before_load:
cmd = self.before_load
print(f"[strata] before loading: {cmd if isinstance(cmd, str) else ' '.join(map(str, cmd))}", flush=True)
+ why = None
try:
- subprocess.run(cmd, shell=isinstance(cmd, str), timeout=120, stdin=subprocess.DEVNULL)
+ r = subprocess.run(cmd, shell=isinstance(cmd, str), timeout=self.before_load_timeout_s,
+ stdin=subprocess.DEVNULL)
+ if r.returncode != 0:
+ why = f"it exited with code {r.returncode}"
+ except subprocess.TimeoutExpired:
+ why = f"it did not finish in {self.before_load_timeout_s:g} s (before_load_timeout_s)"
except (OSError, subprocess.SubprocessError) as e:
- print(f"[strata] the before_load command failed ({e}); loading anyway", flush=True)
- if self.min_free_vram_mib:
+ why = f"it could not run ({e})"
+ if why is not None:
+ # #1997: a failing hook (e.g. the other server's /unload that is still busy) may mean the GPU was not
+ # given back. A peer that is simply not running also makes `curl` fail (exit 7), so the exit code alone
+ # does not refuse: the card's free VRAM decides (min_free_vram_mib, checked just below); "strict" always
+ # refuses, "ignore" always loads.
+ if self.before_load_strict and not self.before_load_ignore_failure:
+ print(f"[strata] the before_load command failed ({why}); the model stays unloaded and this "
+ "request is answered 503 - try again (before_load_strict)", flush=True)
+ raise GpuBusy(f"the before_load command failed ({why}): the GPU was not given back, so the "
+ "model was not loaded; try again in a moment", retry_after=15)
+ hook_failed = why
+ if self.before_load_ignore_failure or not self.min_free_vram_mib:
+ print(f"[strata] the before_load command failed ({why}); loading anyway"
+ + ("" if self.before_load_ignore_failure else
+ " (set min_free_vram_mib to have the free VRAM checked, or before_load_strict to refuse)"),
+ flush=True)
+ else:
+ print(f"[strata] the before_load command failed ({why}); checking the free VRAM", flush=True)
+ if self.min_free_vram_mib and not (hook_failed and self.before_load_ignore_failure):
free = self.free_vram_mib()
- deadline = time.time() + 15 # memory another process just gave back can take a moment
+ deadline = time.time() + self.vram_free_wait_s # memory another process just gave back can take a moment
while free is not None and free < self.min_free_vram_mib and time.time() < deadline:
time.sleep(0.5)
free = self.free_vram_mib()
if free is not None and free < self.min_free_vram_mib:
+ if hook_failed and not self.before_load_ignore_failure:
+ print(f"[strata] the before_load command failed ({hook_failed}) and only {free} MiB of VRAM is "
+ f"free (the model needs {self.min_free_vram_mib}): not loading; 503", flush=True)
raise GpuBusy(f"the GPU is in use by another program: {free} MiB of VRAM free, the model needs "
- f"{self.min_free_vram_mib} (min_free_vram_mib) - it stays unloaded until that is free")
+ f"{self.min_free_vram_mib} (min_free_vram_mib) - it stays unloaded until that is free"
+ + (f"; the before_load command failed ({hook_failed})" if hook_failed else ""),
+ retry_after=15)
+ if hook_failed and not self.before_load_ignore_failure:
+ print(f"[strata] the before_load command failed ({hook_failed}), but {free} MiB of VRAM is free: "
+ "loading", flush=True)
vision_started = False
if self._vision_down(): # first, as at a start: a GPU encoder takes its VRAM before
print("[strata] starting the vision encoder ...", flush=True) # the engine sizes its cache
@@ -3158,6 +3217,18 @@ class Service:
with self.status_lock:
self.status["queued"] -= 1
+ UNLOAD_WAIT_S = 300.0 # #1997: POST /unload?wait=1 waits this long for the running turn (wait=<s> to choose)
+
+ def unload_waiting(self, wait_s: float) -> str:
+ """#1997: unload(), but a running or queued request is waited for (up to wait_s) instead of answering "busy":
+ a peer server that needs this GPU can then free the card on the server's terms. "busy" after wait_s."""
+ deadline = time.monotonic() + max(0.0, wait_s)
+ while True:
+ r = self.unload()
+ if r != "busy" or time.monotonic() >= deadline:
+ return r
+ time.sleep(0.25)
+
def unload(self, idle_for: float | None = None) -> str:
"""Stop the engine between requests: "unloaded", "not loaded", "busy" (a request is running or waiting, or
with idle_for: one ran more recently than that) or "unsupported"."""
@@ -4846,7 +4917,7 @@ def make_handler(svc: Service):
self.send_header("Content-Length", "0")
self.end_headers()
- def _json(self, code, obj):
+ def _json(self, code, obj, headers=None):
if self.record is not None:
with svc.status_lock:
self.record["http_status"] = code
@@ -4858,6 +4929,8 @@ def make_handler(svc: Service):
body = json.dumps(obj, ensure_ascii=False).encode()
self.send_response(code)
self.send_header("Content-Type", "application/json")
+ for k, v in (headers or {}).items():
+ self.send_header(k, str(v))
self._cors()
self.send_header("Content-Length", str(len(body)))
self.end_headers()
@@ -5049,7 +5122,9 @@ def make_handler(svc: Service):
return
if path == "/unload": # give the GPU back now (between requests)
try:
- r = svc.unload()
+ wait_s = unload_wait(parse_qs(urlsplit(self.path).query).get("wait", [None])[0],
+ svc.UNLOAD_WAIT_S)
+ r = svc.unload_waiting(wait_s) if wait_s else svc.unload()
except EngineStuck as e:
self._json(503, {"error": {"type": "server_error", "message": str(e)}})
return
@@ -5060,7 +5135,9 @@ def make_handler(svc: Service):
svc.load()
self._json(200, {"status": "loaded"})
except GpuBusy as e:
- self._json(503, {"error": {"type": "server_error", "message": str(e)}})
+ ra = getattr(e, "retry_after", None)
+ self._json(503, {"error": {"type": "server_error", "message": str(e)}},
+ headers={"Retry-After": int(ra)} if ra else None)
return
try:
req = json.loads(self._body() or b"{}")
@@ -5077,7 +5154,9 @@ def make_handler(svc: Service):
self._json(404, {"error": {"message": "model not found"}})
return
if path == "/v1/unload":
- result = svc.unload()
+ wait_s = unload_wait(parse_qs(urlsplit(self.path).query).get("wait", [None])[0],
+ svc.UNLOAD_WAIT_S)
+ result = svc.unload_waiting(wait_s) if wait_s else svc.unload()
if result == "busy":
raise ModelBusy("a request is running or queued")
else:
@@ -5142,7 +5221,9 @@ def make_handler(svc: Service):
self._json(502, {"error": {"type": "structured_output_failed", "code": "structured_output_failed",
"message": str(e)}})
except (GpuBusy, EngineStarting) as e:
- self._json(503, {"error": {"type": "server_error", "message": str(e)}})
+ ra = getattr(e, "retry_after", None) # #1997 / #1994: a refusal that is worth retrying
+ self._json(503, {"error": {"type": "server_error", "message": str(e)}},
+ headers={"Retry-After": int(ra)} if ra else None)
except (TypeError, KeyError, AttributeError, IndexError, UnicodeError) as e:
# a body that is JSON but the wrong shape ("messages": 5, a content part that is a number): a 400 that
# says so, with the traceback in the log, instead of a dropped connection - unless the answer began
@@ -6375,6 +6456,13 @@ def main() -> int:
svc.min_free_vram_mib = a.min_free_vram_mib if a.min_free_vram_mib is not None else \
int(cfg.get("min_free_vram_mib") or 0)
svc.before_load = a.before_load or cfg.get("before_load") or None
+ try:
+ svc.before_load_timeout_s = float(cfg.get("before_load_timeout_s") or svc.before_load_timeout_s)
+ except (TypeError, ValueError):
+ print("[strata] \"before_load_timeout_s\" in the config is not a number; using "
+ f"{svc.before_load_timeout_s:g} s", flush=True)
+ svc.before_load_ignore_failure = cfg.get("before_load_ignore_failure") is True
+ svc.before_load_strict = cfg.get("before_load_strict") is True
# --slot-save-path is relative to the working directory the server was started in; the config's slot_save_path
# to the config's "cwd" (the engine's folder) when it has one. Either way the engine gets an absolute path.
if a.slot_save_path or cfg.get("slot_save_path"):
diff --git a/serve/test_server.py b/serve/test_server.py
index b01bfd369..8f5c73736 100644
--- a/serve/test_server.py
+++ b/serve/test_server.py
@@ -3385,6 +3385,97 @@ class SharingTheGpu(unittest.TestCase):
self.assertEqual(self.chat()[0], 200)
self.assertTrue(mark.exists())
+ def _raw_chat(self):
+ r = urllib.request.Request(self.base + "/v1/chat/completions",
+ data=json.dumps({"model": "m", "messages": [{"role": "user", "content": "hi"}]}).encode(),
+ headers={"Content-Type": "application/json"}, method="POST")
+ try:
+ with urllib.request.urlopen(r, timeout=30) as resp:
+ return resp.status, dict(resp.headers), json.loads(resp.read())
+ except urllib.error.HTTPError as e:
+ with e:
+ return e.code, dict(e.headers), json.loads(e.read())
+
+ def test_failing_before_load_with_free_vram_still_loads(self):
+ """#1997: a peer that is not running makes `curl` exit 7; the card has room, so the model loads."""
+ self.svc.before_load = [sys.executable, "-c", "import sys; sys.exit(7)"]
+ self.svc.min_free_vram_mib = 8000
+ self.svc.free_vram_mib = lambda: 20000
+ self.svc.unload()
+ self.assertEqual(self._raw_chat()[0], 200)
+ self.assertEqual(self.engine.starts, 1)
+
+ def test_failing_before_load_with_full_vram_refuses_with_503_and_retry_after(self):
+ self.svc.before_load = [sys.executable, "-c", "import sys; sys.exit(3)"]
+ self.svc.min_free_vram_mib = 8000
+ self.svc.free_vram_mib = lambda: 2000
+ self.svc.unload()
+ self.svc.vram_free_wait_s = 0.2 # (not the 15 s wait for memory)
+ status, headers, body = self._raw_chat()
+ self.assertEqual(status, 503)
+ self.assertIn("exited with code 3", body["error"]["message"])
+ self.assertTrue(int(headers["Retry-After"]) > 0)
+ self.assertFalse(self.engine.alive())
+ self.assertEqual(self.engine.starts, 0)
+ self.svc.free_vram_mib = lambda: 20000 # the peer let go: the next try loads
+ self.assertEqual(self._raw_chat()[0], 200)
+
+ def test_before_load_strict_refuses_even_with_free_vram(self):
+ self.svc.before_load = [sys.executable, "-c", "import sys; sys.exit(7)"]
+ self.svc.before_load_strict = True
+ self.svc.min_free_vram_mib = 8000
+ self.svc.free_vram_mib = lambda: 20000
+ self.svc.unload()
+ status, headers, body = self._raw_chat()
+ self.assertEqual(status, 503)
+ self.assertTrue(int(headers["Retry-After"]) > 0)
+ self.assertEqual(self.engine.starts, 0)
+
+ def test_failing_before_load_without_a_vram_limit_loads(self):
+ self.svc.before_load = [sys.executable, "-c", "import sys; sys.exit(7)"]
+ self.svc.unload()
+ self.assertEqual(self._raw_chat()[0], 200)
+
+ def test_before_load_timeout_is_configurable_and_a_timeout_refuses(self):
+ self.svc.before_load = [sys.executable, "-c", "import time; time.sleep(30)"]
+ self.svc.before_load_timeout_s = 0.5
+ self.svc.before_load_strict = True
+ self.svc.unload()
+ t0 = time.time()
+ status, headers, body = self._raw_chat()
+ self.assertEqual(status, 503)
+ self.assertLess(time.time() - t0, 10)
+ self.assertIn("before_load_timeout_s", body["error"]["message"])
+ self.assertFalse(self.engine.alive())
+
+ def test_before_load_failure_can_be_ignored_by_config(self):
+ self.svc.before_load = [sys.executable, "-c", "import sys; sys.exit(1)"]
+ self.svc.before_load_ignore_failure = True
+ self.svc.min_free_vram_mib = 8000
+ self.svc.free_vram_mib = lambda: 2000 # full, but ignore means load
+ self.svc.unload()
+ self.assertEqual(self._raw_chat()[0], 200)
+
+ def test_unload_wait_waits_for_the_running_turn(self):
+ """#1997: POST /unload answers busy at once (409); with ?wait=1 it waits for the turn and then unloads."""
+ self.svc.fifo.acquire()
+ threading.Timer(1.0, self.svc.fifo.release).start()
+ t0 = time.time()
+ self.assertEqual(self.req("/unload", {}), (409, {"status": "busy"}))
+ self.assertLess(time.time() - t0, 0.9)
+ status, body = self.req("/unload?wait=1", {})
+ self.assertEqual((status, body), (200, {"status": "unloaded"}))
+ self.assertGreaterEqual(time.time() - t0, 0.9)
+ self.assertFalse(self.engine.alive())
+
+ def test_unload_wait_gives_up_after_the_given_seconds(self):
+ with self.svc.fifo:
+ t0 = time.time()
+ self.assertEqual(self.req("/unload?wait=0.6", {}),
+ (409, {"status": "busy"}))
+ self.assertLess(time.time() - t0, 5)
+ self.assertTrue(self.engine.alive())
+
def test_vision_encoder_unloads_and_starts_first(self):
order = []