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: the unread body is dropped under a time limit only, so megabytes still get their answer
Guillaume PUTIERa8497cf157ed
diff --git a/serve/server.py b/serve/server.pyindex 2c11de079..62305b1e3 100644--- a/serve/server.py+++ b/serve/server.py@@ -2990,7 +2990,7 @@ def make_handler(svc: Service):record = None # #332: this request's monitor record, if keptwatch_done = None # #430 #431: stops this request's disconnect watcherbody_read = False # whether a handler took this request's body- DRAIN_LIMIT = 1 << 20 # the most an unread body is read and dropped+ DRAIN_SECONDS = 5 # the longest an unread body is read and droppeddef log_message(self, fmt, *args):pass@@ -3007,7 +3007,9 @@ def make_handler(svc: Service):"""An answer sent before the body was read (a 401, a 403, /load, a method with no handler) must not closethe connection on unread bytes: the close then sends a reset, and a client that sends its body after theheaders (http.client, urllib, requests) gets a connection error instead of the answer. So the body is- read and dropped here, once, after an answer. A body over DRAIN_LIMIT is left unread."""+ read and dropped here, once, after an answer, in pieces so that its size is never held in memory. What+ has not arrived DRAIN_SECONDS later is left unread: the limit is time, so that a conversation of many+ megabytes, at any speed the client has, still gets its answer."""headers = getattr(self, "headers", None)if self.body_read or headers is None:return@@ -3015,11 +3017,16 @@ def make_handler(svc: Service):left = int(headers.get("Content-Length", 0))except ValueError:return- if not 0 < left <= self.DRAIN_LIMIT:+ if left <= 0:return- self.connection.settimeout(5) # a client that never sends what it announced+ deadline = time.monotonic() + self.DRAIN_SECONDStry:- self.rfile.read(left)+ while left > 0 and (wait := deadline - time.monotonic()) > 0:+ self.connection.settimeout(wait) # a client that never sends what it announced+ piece = self.rfile.read1(min(left, 1 << 20)) # one socket read: read() would wait for it all+ if not piece:+ break+ left -= len(piece)except OSError:passdiff --git a/serve/test_server.py b/serve/test_server.pyindex 6efa48d8a..f5c1d445e 100644--- a/serve/test_server.py+++ b/serve/test_server.py@@ -3051,6 +3051,38 @@ class AnswerBeforeTheBody(unittest.TestCase):finally:self.svc.api_key = ""+ def test_a_wrong_key_and_a_body_of_megabytes(self):+ """An agent client's conversation, or one screenshot, is several MiB, and a rotated key is when the 401 matters."""+ self.svc.api_key = "secret"+ try:+ self.assertEqual(self.status("POST", "/v1/chat/completions", body=b'{"x": "' + b"a" * (80 << 20) + b'"}'),+ "HTTP/1.0 401 Unauthorized")+ finally:+ self.svc.api_key = ""++ def test_a_body_that_comes_in_drops_does_not_hold_the_connection(self):+ """A client that announces a body and sends it a byte at a time is let go after DRAIN_SECONDS, with its+ answer: the time limit is on the whole body, not on each read."""+ self.svc.api_key = "secret"+ try:+ with mock.patch.object(self.httpd.RequestHandlerClass, "DRAIN_SECONDS", 0.5), \+ socket.create_connection(("127.0.0.1", self.port), timeout=10) as s:+ s.sendall((f"POST /v1/chat/completions HTTP/1.1\r\nHost: 127.0.0.1:{self.port}\r\n"+ f"Content-Length: {1 << 20}\r\n\r\n").encode())+ started, answer = time.monotonic(), b""+ s.settimeout(0.2)+ while True:+ try:+ if not (chunk := s.recv(65536)):+ break+ answer += chunk+ except TimeoutError:+ s.sendall(b"a") # a byte every 0.2 s keeps each read of the server alive+ self.assertEqual(answer.split(b"\r\n", 1)[0], b"HTTP/1.0 401 Unauthorized")+ self.assertLess(time.monotonic() - started, 5)+ finally:+ self.svc.api_key = ""+def test_load_and_unload(self):self.assertEqual(self.status("POST", "/unload"), "HTTP/1.0 200 OK")self.assertEqual(self.status("POST", "/load"), "HTTP/1.0 200 OK")