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: keep-alive while a tool call's held-back argument is generated (#1666)

nekomario2814b9dd2e52af
diff --git a/serve/server.py b/serve/server.py
index c21f335af..19811fc11 100644
--- a/serve/server.py
+++ b/serve/server.py
@@ -198,6 +198,7 @@ def focused_recovery_prompt(tok, ids, generated):
LOOPBACK_NAMES = ("localhost", "127.0.0.1", "::1")
QUEUE_BEAT_S = 10.0 # a request waiting for the single turn sends a keep-alive this often (#1619)
+KEEPALIVE_S = 10.0 # a held-back tool argument leaves the stream quiet: ping at least this often
CTX_SLACK = 8 # `strata --serve` rejects prompt + max_new + 8 > context: keep the same margin here
# The live tok/s is a rate over a window, not a mean since the first token: a mean reads ~1/elapsed at the first
# token (the Monitor showed five-digit numbers) and then undershoots for the first second of every answer.
@@ -3672,9 +3673,15 @@ class Service:
seg, wrap, leaving = [], False, False # this pass's tokens; the budget is reached; closed
opens = False # the thinking is over: write the forced call's opening
try:
+ # A value the frontend holds back (a tool call's array or object argument is sent
+ # whole, once complete) can keep the stream silent for minutes while tokens are
+ # generated; a client's idle timeout then ends the request. Send the same keep-alive
+ # the engine's heartbeat sends when nothing has gone out for KEEPALIVE_S.
+ last_out = time.perf_counter()
for t in gen:
if t is None: # heartbeat while the engine is quiet
last_print = self._progress(last_print, st=st)
+ last_out = time.perf_counter()
yield "ping", None
continue
n += 1
@@ -3707,6 +3714,11 @@ class Service:
if ev.kind in ("content", "tool_start", "tool_call"):
answered = True
yield "event", ev
+ if evs:
+ last_out = time.perf_counter()
+ elif time.perf_counter() - last_out >= KEEPALIVE_S:
+ last_out = time.perf_counter()
+ yield "ping", None
if stops is not None and stops.hit is not None:
finish = "stop" # gen.close() below STOPs the engine, as for a stop token
break
diff --git a/serve/test_server.py b/serve/test_server.py
index ccd8408c7..c35998736 100644
--- a/serve/test_server.py
+++ b/serve/test_server.py
@@ -2271,6 +2271,42 @@ class CancelledRead(unittest.TestCase):
self.assertEqual(m["totals"]["prompt_tokens"], 8 + 2 * total)
+class HeldBackArgKeepalive(unittest.TestCase):
+ """#1666: with stream_tools a tool call's array or object argument is held until it is whole, so a big one
+ left the stream silent for minutes while tokens were generated - a client's idle timeout (Forge's is 120 s)
+ then ended a healthy request. The token loop now sends the engine's own keep-alive ping whenever nothing has
+ gone out for KEEPALIVE_S; a quick answer is untouched."""
+
+ TOOLS = [{"name": "f", "parameters": {"properties": {}}}]
+
+ def test_a_held_back_call_still_pings(self):
+ tok = ByteTokenizer()
+ clock = [0.0]
+
+ class HeldEngine(MockEngine): # ~1 s to generate each token, none of it leaving the stream
+ def generate(self, ids, max_new, sampling, cancel, embeddings=None):
+ for t in super().generate(ids, max_new, sampling, cancel):
+ clock[0] += 1.0
+ yield t
+
+ # a call inside the thinking is held whole until </tool_call> (the frontend's rcall state): every token
+ # after the opener produces no stream event - the same silence a long array argument makes
+ svc = Service(HeldEngine(tok, "<tool_call><function=f>" + "x" * 30, max_context=CTX), tok,
+ ChatTemplate(ROOT / "serve/chat_template.jinja"))
+ with mock.patch("serve.server.time.perf_counter", lambda: clock[0]):
+ kinds = [k for k, _ in svc.run(tok.encode("hi"), True, self.TOOLS, 60, {}, threading.Event())]
+ self.assertIn("ping", kinds)
+ self.assertIn("done", kinds)
+
+ def test_an_ordinary_answer_does_not_ping(self):
+ tok = ByteTokenizer()
+ svc = Service(MockEngine(tok, "ok", max_context=CTX), tok,
+ ChatTemplate(ROOT / "serve/chat_template.jinja"))
+ kinds = [k for k, _ in svc.run(tok.encode("hi"), False, None, 10, {}, threading.Event())]
+ self.assertNotIn("ping", kinds)
+ self.assertIn("done", kinds)
+
+
class LiveRate(unittest.TestCase):
"""The Monitor's Speed readout: live.tok_s is a rate, and a request that never got a DONE keeps no counters.