Skip to content

Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.

GLM and MiMo servers: stop the job when the client disconnects

YanissAmz39fd82b408ff
diff --git a/tools/glm/serve.py b/tools/glm/serve.py
index 1423b38..186bbeb 100644
--- a/tools/glm/serve.py
+++ b/tools/glm/serve.py
@@ -277,8 +277,12 @@ class ResidentEngine:
return int(self.tokenizer.encode(text, encode_special_tokens=True).numel())
async def generate(self, prompt: str, *, max_tokens: int, temperature: float,
- top_p: float, stop: list[str], sampler_kw: dict | None = None) -> AsyncIterator[str]:
- """Yield incremental decoded text. Caller serializes access to this method."""
+ top_p: float, stop: list[str], sampler_kw: dict | None = None,
+ cancel: asyncio.Event | None = None) -> AsyncIterator[str]:
+ """Yield incremental decoded text. Caller serializes access to this method.
+
+ When `cancel` is set (client gone) the job is cancelled between two generator steps. A step is one
+ prefill chunk or one decode step, so a drop during prefill stops at the next chunk boundary."""
import time
import torch
from exllamav3.generator.sampler import ComboSampler, GreedySampler
@@ -288,6 +292,8 @@ class ResidentEngine:
if getattr(self, "slot_store", None) is not None:
self.slot_store.note_prompt(prompt, ids)
self.last_stats = {}
+ if cancel is not None and cancel.is_set():
+ return # the client left while the request was queued: no prefill
kw = dict(sampler_kw if sampler_kw is not None else DEFAULT_SAMPLER_KW) # rep_p / pres_p / dry_* (loop2)
if temperature == 0:
sampler = ComboSampler(temperature=0.0, **kw) if kw else GreedySampler()
@@ -299,21 +305,30 @@ class ResidentEngine:
stop_conditions=conditions)
generator.enqueue(job)
loop = asyncio.get_running_loop()
- while generator.num_remaining_jobs():
- batch = await loop.run_in_executor(None, lambda: list(generator.iterate()))
- for event in batch:
- if event.get("error"):
- raise RuntimeError(str(event["error"]))
- text = event.get("text", "")
- if text:
- yield text
- if event.get("eos"):
- self.last_stats = event
- if probe is not None:
- probe.request(int(ids.numel()), int(event.get("new_tokens", 0)),
- time.perf_counter() - t0)
+ try:
+ while generator.num_remaining_jobs():
+ if cancel is not None and cancel.is_set():
+ generator.cancel(job)
return
- torch.cuda.synchronize()
+ batch = await loop.run_in_executor(None, lambda: list(generator.iterate()))
+ for event in batch:
+ if event.get("error"):
+ raise RuntimeError(str(event["error"]))
+ text = event.get("text", "")
+ if text:
+ yield text
+ if event.get("eos"):
+ self.last_stats = event
+ if probe is not None:
+ probe.request(int(ids.numel()), int(event.get("new_tokens", 0)),
+ time.perf_counter() - t0)
+ return
+ torch.cuda.synchronize()
+ finally:
+ # Whatever way the call ends (error, client gone, cancelled task) no job stays in the Generator:
+ # a leftover job would run inside the next request.
+ if generator.num_remaining_jobs():
+ generator.clear_queue()
def warmup(engine: Any, template: str, prompt_tokens: int = 4608, decode_tokens: int = 16) -> float:
@@ -449,6 +464,11 @@ def create_app(engine: Any, model_id: str, template: str) -> web.Application:
async def worker() -> None:
while True:
body, out = await queue.get()
+ cancel = body.get("_cancel")
+ if cancel is not None and cancel.is_set(): # the client left while the job was queued: no prefill, no decode
+ out.put_nowait(None)
+ queue.task_done()
+ continue
store = getattr(engine, "slot_store", None)
try:
async with lock:
@@ -457,7 +477,8 @@ def create_app(engine: Any, model_id: str, template: str) -> web.Application:
async for delta in engine.generate(
body["_prompt"],
max_tokens=token_limit(body), temperature=body.get("temperature", 0.0),
- top_p=body.get("top_p", 1.0), stop=body.get("_stop", [])):
+ top_p=body.get("top_p", 1.0), stop=body.get("_stop", []),
+ cancel=body.get("_cancel")):
out.put_nowait(delta)
out.put_nowait(None)
except Exception as exc:
@@ -473,6 +494,30 @@ def create_app(engine: Any, model_id: str, template: str) -> web.Application:
async def close(app_: web.Application) -> None:
app_["worker_task"].cancel()
+ async def drain(request: web.Request, cancel: asyncio.Event, out: asyncio.Queue) -> AsyncIterator[str]:
+ """Items the worker produced for one request. Nothing is written to the client while a job is
+ queued or prefilling (and never, for a non-streaming reply), so a client that hung up is noticed
+ by polling the transport. Raises ConnectionResetError and sets `cancel` when it has left."""
+ while True:
+ tr = request.transport
+ if tr is None or tr.is_closing():
+ cancel.set()
+ raise ConnectionResetError("client left")
+ try:
+ item = await asyncio.wait_for(out.get(), 0.5)
+ except asyncio.TimeoutError:
+ continue
+ if item is None:
+ return
+ if isinstance(item, Exception):
+ raise item
+ yield item
+
+ def client_left(cancel: asyncio.Event, path: str) -> web.Response:
+ cancel.set()
+ print(f"serve: client left {path}, job cancelled", file=sys.stderr, flush=True)
+ return web.Response(status=499)
+
async def completions(request: web.Request) -> web.StreamResponse:
try:
body = await request.json()
@@ -504,14 +549,12 @@ def create_app(engine: Any, model_id: str, template: str) -> web.Application:
return base | {"choices": [{"index": 0, "delta": delta, "finish_reason": finish}]}
out: asyncio.Queue = asyncio.Queue()
+ cancel = body["_cancel"] = asyncio.Event()
await queue.put((body, out))
prefix = "<think>" if opens_thinking(prompt) else ""
- async def deltas() -> AsyncIterator[str]:
- while (item := await out.get()) is not None:
- if isinstance(item, Exception):
- raise item
- yield item
+ def deltas() -> AsyncIterator[str]:
+ return drain(request, cancel, out)
def usage(raw: str) -> dict[str, int]:
completion_tokens = engine.count_tokens(raw)
@@ -576,9 +619,14 @@ def create_app(engine: Any, model_id: str, template: str) -> web.Application:
{"usage": usage(text[len(prefix):]), "timings": timings(text)}))
await response.write(b"data: [DONE]\n\n")
await response.write_eof()
- except (ConnectionResetError, asyncio.CancelledError):
- raise # client left: nothing to send
+ except ConnectionResetError:
+ client_left(cancel, "stream") # client left: nothing to send
+ return response
+ except asyncio.CancelledError:
+ cancel.set()
+ raise
except Exception as exc: # noqa: BLE001
+ cancel.set()
# Fail inside the stream, but still end it the OpenAI way: clients such as OpenCode report
# "stream ended without finish_reason" and retry when the connection just drops.
try:
@@ -590,7 +638,16 @@ def create_app(engine: Any, model_id: str, template: str) -> web.Application:
pass
return response
- raw = "".join([d async for d in deltas()])
+ try:
+ raw = "".join([d async for d in deltas()])
+ except ConnectionResetError:
+ return client_left(cancel, "request")
+ except asyncio.CancelledError:
+ cancel.set()
+ raise
+ except Exception:
+ cancel.set()
+ raise
text, _ = stop_text(prefix + raw, body["_stop"])
message = parse_completion(text)
finish = finish_for(message, text[len(prefix):])
@@ -647,12 +704,20 @@ def create_app(engine: Any, model_id: str, template: str) -> web.Application:
raise web.HTTPBadRequest(reason=str(exc)) from exc
out: asyncio.Queue = asyncio.Queue()
+ cancel = job["_cancel"] = asyncio.Event()
await queue.put((job, out))
text = ""
- while (item := await out.get()) is not None:
- if isinstance(item, Exception):
- raise item
- text += item
+ try:
+ async for item in drain(request, cancel, out):
+ text += item
+ except ConnectionResetError:
+ return client_left(cancel, "completion")
+ except asyncio.CancelledError:
+ cancel.set()
+ raise
+ except Exception:
+ cancel.set()
+ raise
text, stopped = stop_text(text, job["_stop"])
st = getattr(engine, "last_stats", None) or {}
return web.json_response({
diff --git a/tools/glm/test_serve.py b/tools/glm/test_serve.py
index c17a5e5..effda68 100644
--- a/tools/glm/test_serve.py
+++ b/tools/glm/test_serve.py
@@ -241,5 +241,79 @@ class ServeTests(unittest.TestCase):
self.assertIn("usage", chunks[-1])
+ def test_client_disconnect_cancels_the_job(self):
+ """Stream and non-stream drops stop generation within a few steps, a job cancelled while queued
+ never runs, and the request after them is served."""
+ from aiohttp.test_utils import TestClient, TestServer
+
+ class SlowEngine(FakeEngine):
+ steps = 0
+ stopped = None
+
+ async def generate(self, prompt, **kwargs):
+ self.prompts.append(prompt)
+ cancel = kwargs.get("cancel")
+ if "slow" not in prompt:
+ yield "ok"
+ return
+ self.stopped = None
+ for _ in range(1000):
+ if cancel is not None and cancel.is_set():
+ self.stopped = "cancelled"
+ return
+ self.steps += 1
+ await asyncio.sleep(0.01)
+ yield "t "
+ self.stopped = "finished"
+
+ def body(text, stream):
+ return {"model": "m", "stream": stream, "messages": [{"role": "user", "content": text}]}
+
+ async def settle(engine, want_steps_at_most):
+ for _ in range(200):
+ if engine.stopped is not None:
+ break
+ await asyncio.sleep(0.02)
+ self.assertEqual(engine.stopped, "cancelled")
+ self.assertLess(engine.steps, want_steps_at_most)
+
+ async def check():
+ engine = SlowEngine("x")
+ client = TestClient(TestServer(serve.create_app(engine, "m", self.template)))
+ await client.start_server()
+ # 1. streaming drop mid-stream
+ response = await client.post("/v1/chat/completions", json=body("slow a", True))
+ await response.content.readany()
+ await asyncio.sleep(0.15)
+ response.close()
+ await settle(engine, 100)
+ # 2. a following request is served at once
+ ok = await asyncio.wait_for(client.post("/v1/chat/completions", json=body("hi", False)), 5)
+ self.assertEqual(ok.status, 200)
+ self.assertEqual((await ok.json())["choices"][0]["message"]["content"], "ok")
+ # 3. non-streaming drop
+ engine.steps = 0
+ try:
+ await asyncio.wait_for(client.post("/v1/chat/completions", json=body("slow b", False)), 0.3)
+ except asyncio.TimeoutError:
+ pass
+ await settle(engine, 100)
+ # 4. a job cancelled while queued never reaches the engine
+ engine.steps, engine.prompts = 0, []
+ first = await client.post("/v1/chat/completions", json=body("slow c", True))
+ await first.content.readany()
+ try:
+ await asyncio.wait_for(client.post("/v1/chat/completions", json=body("slow queued", False)), 0.3)
+ except asyncio.TimeoutError:
+ pass
+ first.close()
+ ok = await asyncio.wait_for(client.post("/v1/chat/completions", json=body("hi again", False)), 5)
+ self.assertEqual(ok.status, 200)
+ self.assertFalse(any("slow queued" in p for p in engine.prompts))
+ await client.close()
+
+ run(check())
+
+
if __name__ == "__main__":
unittest.main()
diff --git a/tools/mimo/serve.py b/tools/mimo/serve.py
index 12482a5..094453b 100644
--- a/tools/mimo/serve.py
+++ b/tools/mimo/serve.py
@@ -252,14 +252,20 @@ class ResidentEngine:
self.generator.spec_gate = SpecGate()
async def generate(self, prompt: str, *, max_tokens: int, temperature: float, top_p: float,
- stop: list[str], reset_gate: bool = False) -> AsyncIterator[str]:
- """Yield incremental decoded text. The caller serializes access to this method."""
+ stop: list[str], reset_gate: bool = False,
+ cancel: asyncio.Event | None = None) -> AsyncIterator[str]:
+ """Yield incremental decoded text. The caller serializes access to this method.
+
+ When `cancel` is set (client gone) the job is cancelled between two generator steps. A step is one
+ prefill chunk or one decode step, so a drop during prefill stops at the next chunk boundary."""
import torch # noqa: F401 (keeps the ROCm runtime loaded for the worker thread)
from exllamav3.generator.sampler import ComboSampler, GreedySampler
ids = self.tokenizer.encode(prompt, encode_special_tokens=True)
if getattr(self, "slot_store", None) is not None:
self.slot_store.note_prompt(prompt, ids)
self.last_stats = {}
+ if cancel is not None and cancel.is_set():
+ return # the client left while the request was queued: no prefill
sampler = (GreedySampler() if temperature == 0 else
ComboSampler(temperature=max(temperature, 1e-6), top_p=top_p, min_p=0.0))
if reset_gate:
@@ -268,18 +274,27 @@ class ResidentEngine:
stop_conditions=list(self.config.eos_token_id_list) + stop)
self.generator.enqueue(job)
loop = asyncio.get_running_loop()
- while self.generator.num_remaining_jobs():
- batch = await loop.run_in_executor(None, lambda: list(self.generator.iterate()))
- for event in batch:
- if event.get("error"):
- raise RuntimeError(str(event["error"]))
- text = event.get("text", "")
- if text:
- yield text
- if event.get("eos"):
- self.last_stats = event
+ try:
+ while self.generator.num_remaining_jobs():
+ if cancel is not None and cancel.is_set():
+ self.generator.cancel(job)
return
- torch.cuda.synchronize()
+ batch = await loop.run_in_executor(None, lambda: list(self.generator.iterate()))
+ for event in batch:
+ if event.get("error"):
+ raise RuntimeError(str(event["error"]))
+ text = event.get("text", "")
+ if text:
+ yield text
+ if event.get("eos"):
+ self.last_stats = event
+ return
+ torch.cuda.synchronize()
+ finally:
+ # Whatever way the call ends (error, client gone, cancelled task) no job stays in the Generator:
+ # a leftover job would run inside the next request.
+ if self.generator.num_remaining_jobs():
+ self.generator.clear_queue()
def load_prior(self, cal) -> None:
"""Seed the confidence calibrator from draft_conf_prior.json in the drafter directory (EXL3_DRAFT_PRIOR=0 skips it)."""
@@ -319,6 +334,11 @@ def create_app(engine: Any, model_id: str, template: str) -> web.Application:
async def worker() -> None:
while True:
body, out = await queue.get()
+ cancel = body.get("_cancel")
+ if cancel is not None and cancel.is_set(): # the client left while the job was queued: no prefill, no decode
+ out.put_nowait(None)
+ queue.task_done()
+ continue
store = getattr(engine, "slot_store", None)
try:
async with lock:
@@ -329,7 +349,8 @@ def create_app(engine: Any, model_id: str, template: str) -> web.Application:
max_tokens=token_limit(body),
temperature=body.get("temperature", SERVE_DEFAULTS["temperature"]),
top_p=body.get("top_p", SERVE_DEFAULTS["top_p"]), stop=body.get("_stop", []),
- reset_gate=body.get("_reset_gate", False)):
+ reset_gate=body.get("_reset_gate", False),
+ cancel=body.get("_cancel")):
out.put_nowait(delta)
out.put_nowait(None)
except Exception as exc: # noqa: BLE001
@@ -345,6 +366,30 @@ def create_app(engine: Any, model_id: str, template: str) -> web.Application:
async def close(app_: web.Application) -> None:
app_["worker_task"].cancel()
+ async def drain(request: web.Request, cancel: asyncio.Event, out: asyncio.Queue) -> AsyncIterator[str]:
+ """Items the worker produced for one request. Nothing is written to the client while a job is
+ queued or prefilling (and never, for a non-streaming reply), so a client that hung up is noticed
+ by polling the transport. Raises ConnectionResetError and sets `cancel` when it has left."""
+ while True:
+ tr = request.transport
+ if tr is None or tr.is_closing():
+ cancel.set()
+ raise ConnectionResetError("client left")
+ try:
+ item = await asyncio.wait_for(out.get(), 0.5)
+ except asyncio.TimeoutError:
+ continue
+ if item is None:
+ return
+ if isinstance(item, Exception):
+ raise item
+ yield item
+
+ def client_left(cancel: asyncio.Event, path: str) -> web.Response:
+ cancel.set()
+ print(f"serve: client left {path}, job cancelled", file=sys.stderr, flush=True)
+ return web.Response(status=499)
+
async def completions(request: web.Request) -> web.StreamResponse:
try:
body = await request.json()
@@ -378,14 +423,12 @@ def create_app(engine: Any, model_id: str, template: str) -> web.Application:
return base | {"choices": [{"index": 0, "delta": delta, "finish_reason": finish}]}
out: asyncio.Queue = asyncio.Queue()
+ cancel = body["_cancel"] = asyncio.Event()
await queue.put((body, out))
# MiMo's generation prompt is "<|im_start|>assistant\n": no implicit <think> block.
- async def deltas() -> AsyncIterator[str]:
- while (item := await out.get()) is not None:
- if isinstance(item, Exception):
- raise item
- yield item
+ def deltas() -> AsyncIterator[str]:
+ return drain(request, cancel, out)
def usage(raw: str) -> dict[str, int]:
completion_tokens = engine.count_tokens(raw)
@@ -454,9 +497,14 @@ def create_app(engine: Any, model_id: str, template: str) -> web.Application:
{"usage": usage(text), "timings": timings()}))
await response.write(b"data: [DONE]\n\n")
await response.write_eof()
- except (ConnectionResetError, asyncio.CancelledError):
- raise # client left: nothing to send
+ except ConnectionResetError:
+ client_left(cancel, "stream") # client left: nothing to send
+ return response
+ except asyncio.CancelledError:
+ cancel.set()
+ raise
except Exception as exc: # noqa: BLE001
+ cancel.set()
# Fail inside the stream, but still end it the OpenAI way: clients such as OpenCode report
# "stream ended without finish_reason" and retry when the connection just drops.
try:
@@ -468,7 +516,16 @@ def create_app(engine: Any, model_id: str, template: str) -> web.Application:
pass
return response
- raw = "".join([d async for d in deltas()])
+ try:
+ raw = "".join([d async for d in deltas()])
+ except ConnectionResetError:
+ return client_left(cancel, "request")
+ except asyncio.CancelledError:
+ cancel.set()
+ raise
+ except Exception:
+ cancel.set()
+ raise
text, _ = stop_text(raw, body["_stop"])
message = parse_completion(text)
finish = finish_for(message, text)
@@ -525,12 +582,20 @@ def create_app(engine: Any, model_id: str, template: str) -> web.Application:
raise web.HTTPBadRequest(reason=str(exc)) from exc
out: asyncio.Queue = asyncio.Queue()
+ cancel = job["_cancel"] = asyncio.Event()
await queue.put((job, out))
text = ""
- while (item := await out.get()) is not None:
- if isinstance(item, Exception):
- raise item
- text += item
+ try:
+ async for item in drain(request, cancel, out):
+ text += item
+ except ConnectionResetError:
+ return client_left(cancel, "completion")
+ except asyncio.CancelledError:
+ cancel.set()
+ raise
+ except Exception:
+ cancel.set()
+ raise
text, stopped = stop_text(text, job["_stop"])
st = getattr(engine, "last_stats", None) or {}
return web.json_response({
diff --git a/tools/mimo/test_serve.py b/tools/mimo/test_serve.py
index 09a50ca..1a5f633 100644
--- a/tools/mimo/test_serve.py
+++ b/tools/mimo/test_serve.py
@@ -331,5 +331,79 @@ class ServeTests(unittest.TestCase):
self.assertEqual(engine.resets, 1)
+ def test_client_disconnect_cancels_the_job(self):
+ """Stream and non-stream drops stop generation within a few steps, a job cancelled while queued
+ never runs, and the request after them is served."""
+ from aiohttp.test_utils import TestClient, TestServer
+
+ class SlowEngine(FakeEngine):
+ steps = 0
+ stopped = None
+
+ async def generate(self, prompt, **kwargs):
+ self.prompts.append(prompt)
+ cancel = kwargs.get("cancel")
+ if "slow" not in prompt:
+ yield "ok"
+ return
+ self.stopped = None
+ for _ in range(1000):
+ if cancel is not None and cancel.is_set():
+ self.stopped = "cancelled"
+ return
+ self.steps += 1
+ await asyncio.sleep(0.01)
+ yield "t "
+ self.stopped = "finished"
+
+ def body(text, stream):
+ return {"model": "m", "stream": stream, "messages": [{"role": "user", "content": text}]}
+
+ async def settle(engine, want_steps_at_most):
+ for _ in range(200):
+ if engine.stopped is not None:
+ break
+ await asyncio.sleep(0.02)
+ self.assertEqual(engine.stopped, "cancelled")
+ self.assertLess(engine.steps, want_steps_at_most)
+
+ async def check():
+ engine = SlowEngine("x")
+ client = TestClient(TestServer(serve.create_app(engine, "m", TEMPLATE)))
+ await client.start_server()
+ # 1. streaming drop mid-stream
+ response = await client.post("/v1/chat/completions", json=body("slow a", True))
+ await response.content.readany()
+ await asyncio.sleep(0.15)
+ response.close()
+ await settle(engine, 100)
+ # 2. a following request is served at once
+ ok = await asyncio.wait_for(client.post("/v1/chat/completions", json=body("hi", False)), 5)
+ self.assertEqual(ok.status, 200)
+ self.assertEqual((await ok.json())["choices"][0]["message"]["content"], "ok")
+ # 3. non-streaming drop
+ engine.steps = 0
+ try:
+ await asyncio.wait_for(client.post("/v1/chat/completions", json=body("slow b", False)), 0.3)
+ except asyncio.TimeoutError:
+ pass
+ await settle(engine, 100)
+ # 4. a job cancelled while queued never reaches the engine
+ engine.steps, engine.prompts = 0, []
+ first = await client.post("/v1/chat/completions", json=body("slow c", True))
+ await first.content.readany()
+ try:
+ await asyncio.wait_for(client.post("/v1/chat/completions", json=body("slow queued", False)), 0.3)
+ except asyncio.TimeoutError:
+ pass
+ first.close()
+ ok = await asyncio.wait_for(client.post("/v1/chat/completions", json=body("hi again", False)), 5)
+ self.assertEqual(ok.status, 200)
+ self.assertFalse(any("slow queued" in p for p in engine.prompts))
+ await client.close()
+
+ run(check())
+
+
if __name__ == "__main__":
unittest.main()