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.

Qwen server: shutdown fails the command the driver is running

YanissAmz9db95400f4e7
diff --git a/tools/qwen/serve.py b/tools/qwen/serve.py
index ab9377e..30ab5b0 100644
--- a/tools/qwen/serve.py
+++ b/tools/qwen/serve.py
@@ -786,6 +786,11 @@ class QwenEngine:
fn, args, fut = self._cmds.pop(0)
try:
res = await loop.run_in_executor(None, fn, *args)
+ except asyncio.CancelledError:
+ # shutdown while the command runs: it is no longer in self._cmds, so _drive cannot fail it
+ if not fut.done():
+ fut.set_exception(RuntimeError("the engine driver has stopped"))
+ raise
except Exception as exc: # noqa: BLE001
if not fut.done():
fut.set_exception(exc)
diff --git a/tools/qwen/test_guard.py b/tools/qwen/test_guard.py
index e2edf38..0e0c69a 100644
--- a/tools/qwen/test_guard.py
+++ b/tools/qwen/test_guard.py
@@ -489,6 +489,29 @@ class SessionTests(unittest.TestCase):
self.assertIsInstance(res, RuntimeError)
self.assertIn("driver has stopped", str(res)) # cancelled: not restarted
+ def test_shutdown_while_a_command_runs_fails_it_and_asyncio_run_returns(self):
+ import threading
+ e = make_engine(self.seeded, per_iter=1)
+ e.generator = MultiStubGenerator(self.seeded, per_iter=1)
+ started, release = threading.Event(), threading.Event()
+
+ def slow():
+ started.set()
+ release.wait(5)
+ return "late"
+
+ async def go():
+ cmd = asyncio.ensure_future(e.run_exclusive(slow))
+ while not started.is_set():
+ await asyncio.sleep(0.005)
+ e._driver.cancel()
+ try:
+ await asyncio.wait_for(cmd, 5)
+ finally:
+ release.set()
+ with self.assertRaisesRegex(RuntimeError, "driver has stopped"):
+ asyncio.run(go())
+
def test_cancel_leaves_the_other_session_running(self):
(a, b), gen = self.run_two(cancel_first_after=3)
self.assertIsNone(a)