From 14b9dd2e52af92f0a4880ce37359e9cd7af3983a Mon Sep 17 00:00:00 2001 From: nekomario28 <206304251+nekomario28@users.noreply.github.com> Date: Fri, 9 Oct 2026 16:57:07 +0900 Subject: [PATCH] serve: keep-alive while a tool call's held-back argument is generated (#1666) With stream_tools an array or object argument is held until it is whole, so a large one (a measured 66 KB edit_file call) left the stream silent for minutes while tokens were generated - a client's idle timeout then ended a healthy request (Forge's is 120 s). The token loop now tracks when anything last went out and sends the engine's own keep-alive ping after KEEPALIVE_S (10 s) of silence - the same ping the quiet-engine heartbeat sends, so clients already ignore it. The reporter's reproduction measured a 27.8 s longest gap on a 7 KB array (minutes on the 66 KB one); with this change it is 10.0 s and the call arrives intact. Change and live measurements from the issue; adds the ping-cadence unit test (a patched clock over a held-back ). --- serve/server.py | 12 ++++++++++++ serve/test_server.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/serve/server.py b/serve/server.py index c21f335a..19811fc1 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 ccd8408c..c3599873 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 (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, "" + "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.