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: a Stop during the prompt read goes to the engine at once (#879 abort latency)

Niko1221604056878eda
diff --git a/serve/server.py b/serve/server.py
index 9e669827c..bfc2a4f28 100644
--- a/serve/server.py
+++ b/serve/server.py
@@ -743,6 +743,11 @@ class StrataEngine:
if len(f) >= 3 and f[1].isdigit() and f[2].isdigit():
self.progress = (int(f[1]), int(f[2]))
self.prefill_tok_s_mean = float(f[4]) if len(f) >= 5 else None
+ if not stopped and cancel.is_set():
+ # #879: a Stop during the prompt read went out only after the first token (or after 10 s of
+ # silence, which a stream of PP lines never allows): the engine read the whole prompt first
+ self._send("STOP")
+ stopped = True
yield None
elif line.startswith("DONE"):
self._parse_done(line)
diff --git a/serve/test_control_cancel.py b/serve/test_control_cancel.py
new file mode 100644
index 000000000..2f6b35404
--- /dev/null
+++ b/serve/test_control_cancel.py
@@ -0,0 +1,50 @@
+"""#879: a Stop that arrives while the engine reads the prompt (PP lines, no tokens yet) is sent at once, not after the
+first token."""
+import os
+import queue
+import sys
+import threading
+import unittest
+
+sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+
+from serve import server # noqa: E402
+
+
+class ControlCancelTest(unittest.TestCase):
+ def _engine(self, lines):
+ eng = object.__new__(server.StrataEngine)
+ eng.lines = queue.Queue()
+ for l in lines:
+ eng.lines.put(l)
+ eng.sent = []
+ eng._send = eng.sent.append
+ eng._ctl_mode = "solo"
+ eng._ctl_result = None
+ eng.progress = None
+ eng.prefill_tok_s_mean = None
+ eng._last_done = None
+ eng._parse_done = lambda line: None
+ return eng
+
+ def test_stop_during_prompt_read(self):
+ cancel = threading.Event()
+ eng = self._engine(["PP 256 4096 1 100.0", "PP 512 4096 2 100.0", "PP 768 4096 3 100.0", "DONE 0"])
+ gen = eng._control(cancel, lambda t: None)
+ self.assertIsNone(next(gen)) # first PP line, nothing cancelled yet
+ self.assertEqual(eng.sent, [])
+ cancel.set()
+ self.assertIsNone(next(gen)) # the next PP line: STOP goes out now
+ self.assertEqual(eng.sent, ["STOP"])
+ self.assertIsNone(next(gen))
+ self.assertEqual(eng.sent, ["STOP"]) # once
+
+ def test_no_stop_without_cancel(self):
+ eng = self._engine(["PP 256 4096 1 100.0", "DONE 0"])
+ for _ in eng._control(threading.Event(), lambda t: None):
+ pass
+ self.assertEqual(eng.sent, [])
+
+
+if __name__ == "__main__":
+ unittest.main()