diff --git a/serve/server.py b/serve/server.py index c2333bca..ec736b0f 100644 --- a/serve/server.py +++ b/serve/server.py @@ -31,7 +31,9 @@ import json import os import queue import re +import select import signal +import socket import subprocess import sys import tempfile @@ -1722,10 +1724,36 @@ def make_handler(svc: Service): protocol_version = "HTTP/1.0" # SSE ends by closing the connection record = None # #332: this request's monitor record, if kept + watch_done = None # #430 #431: stops this request's disconnect watcher def log_message(self, fmt, *args): pass + def _watch_client(self, cancel: threading.Event) -> None: + """#430 #431: cancel the request as soon as its client hangs up. A non-streamed request writes nothing + until it ends, and a streamed one only a keep-alive per prompt chunk (and the first write after a hang-up + usually still succeeds), so a dropped request kept the engine busy until its answer or the whole prompt + was done. Every 0.5 s: the socket readable with nothing to read (EOF) means the client closed it. A + request is HTTP/1.0 and fully read here, so no later bytes are expected - data is not a hang-up.""" + done = self.watch_done = threading.Event() + sock = self.connection + + def watch(): + while not done.wait(0.5) and not cancel.is_set(): + try: + readable, _, _ = select.select([sock], [], [], 0) + gone = bool(readable) and sock.recv(1, socket.MSG_PEEK) == b"" + except (ConnectionError, TimeoutError): + gone = True + except (OSError, ValueError): # the socket was closed here: the request has ended + return + if gone: + self._note(outcome="disconnected") + cancel.set() + return + + threading.Thread(target=watch, daemon=True, name="strata-client-watch").start() + def _note(self, **values): """#332: what the monitor shows about this request (nothing when the monitor is off).""" if self.record is not None: @@ -1978,6 +2006,8 @@ def make_handler(svc: Service): self._note(outcome="disconnected") raise # as before #332: the server's own handling finally: + if self.watch_done is not None: + self.watch_done.set() record = self.record if record is not None: with svc.status_lock: @@ -2107,6 +2137,7 @@ def make_handler(svc: Service): ids, thinking, max_new = svc.prepare(messages, tools, kw, max_new) _debug_req("openai", req, messages, tools, max_new, thinking, len(ids)) cancel = threading.Event() + self._watch_client(cancel) # #430 #431 run = run_with_mcp(svc, svc.mcp, messages, tools, kw, ids, thinking, max_new, max_req, req, cancel, {t["name"] for t in extra}) if use_mcp else None chunks = openai_chunks(svc, req, ids, thinking, tools, max_new, cancel, run=run) @@ -2158,6 +2189,7 @@ def make_handler(svc: Service): ids, thinking, max_new = svc.prepare(messages, tools, kw, max_new) _debug_req("anthropic", req, messages, tools, max_new, thinking, len(ids)) cancel = threading.Event() + self._watch_client(cancel) # #430 #431 events = anthropic_events(svc, req, ids, thinking, tools, max_new, cancel) events = self._capture(events, "anthropic") if not req.get("stream"): diff --git a/serve/test_server.py b/serve/test_server.py index 34e8f731..6869de7a 100644 --- a/serve/test_server.py +++ b/serve/test_server.py @@ -593,6 +593,59 @@ class DyingEngine(MockEngine): yield t +class SlowPromptEngine(MockEngine): + """Reads a "long prompt" for up to 20 s without a token (the engine sends nothing then), stopping on cancel.""" + + def generate(self, ids, max_new, sampling, cancel, embeddings=None): + self.cancelled_after = None + t0 = time.monotonic() + while time.monotonic() - t0 < 20: + if cancel.is_set(): + self.cancelled_after = time.monotonic() - t0 + return + time.sleep(0.05) + yield from super().generate(ids, max_new, sampling, cancel, embeddings) + + +class ClientHangUp(unittest.TestCase): + """#430 #431: a client that hangs up during a long prompt read cancels the request within about a second - + non-streamed (which writes nothing until the end) and streamed (one keep-alive per prompt chunk) alike.""" + + @classmethod + def setUpClass(cls): + tok = ByteTokenizer() + cls.engine = SlowPromptEngine(tok, "\n\nOK", max_context=CTX) + cls.svc = Service(cls.engine, tok, ChatTemplate(ROOT / "serve/chat_template.jinja")) + cls.httpd = serve(cls.svc, port=0) + cls.port = cls.httpd.server_address[1] + + @classmethod + def tearDownClass(cls): + cls.httpd.shutdown() + cls.httpd.server_close() + + def hang_up(self, stream): + import socket as so + body = json.dumps({"model": "x", "max_tokens": 20, "stream": stream, + "messages": [{"role": "user", "content": "a long prompt"}]}).encode() + c = so.create_connection(("127.0.0.1", self.port)) + c.sendall(b"POST /v1/chat/completions HTTP/1.1\r\nHost: x\r\nContent-Type: application/json\r\n" + b"Content-Length: " + str(len(body)).encode() + b"\r\n\r\n" + body) + time.sleep(1.0) + c.close() + t0 = time.monotonic() + while self.engine.cancelled_after is None and time.monotonic() - t0 < 10: + time.sleep(0.05) + self.assertIsNotNone(self.engine.cancelled_after, "the request was not cancelled") + self.assertLess(self.engine.cancelled_after, 3.0) + + def test_non_streamed(self): + self.hang_up(False) + + def test_streamed(self): + self.hang_up(True) + + class EngineDeath(unittest.TestCase): """Issue #27: a dead engine is an error (not "length"), and the next request starts it again."""