Skip to content

GLM-5.3-Flash EXL3 on 2x DGX Spark with TensorFold

Python
2.9 KB
Raw Blame
# GET and DELETE requests have their bodies read. The CUDA handler's do_GET and do_DELETE never read a body, so a GET
# or DELETE carrying one (scanners send GETs with a body) left those bytes on the keep-alive connection, and the
# stdlib parsed them as the start of the next request on it: a "Bad request syntax" 400, in HTML with no JSON, for
# whatever came next. Behind a pooling proxy (nginx upstream keepalive) that next request is often another client's.
# Both now call _discard_body (patch 0059's helper, read_body underneath: Content-Length or chunked, a refusal closes
# the connection) before answering, as do_POST's refusals already do. A request without a body reads nothing. A GET or
# DELETE has no use for a body, so the read is bounded at 64 KiB (not the 32 MiB a POST may carry): a larger one is
# refused unread and the connection closed, so a stream of big GET bodies cannot make the server buffer them.
# tools/test_get_bodies.py.

--- tensorfold/cuda/http.py
+++ tensorfold/cuda/http.py
@@ -15,7 +15,7 @@
 from tensorfold.server.cancellation import RequestCancelled, socket_cancellation
 from tensorfold.server.errors import CapacityError, RequestError, admission_response, error_body
 from tensorfold.server.http import Server
-from tensorfold.server.request_body import read_body
+from tensorfold.server.request_body import LIMIT, read_body
 from tensorfold.server.stacks import Rearming
 
 if TYPE_CHECKING:
@@ -23,6 +23,7 @@
 
 
 _TOKENIZER_ROUTES = ("/tokenize", "/v1/tokenize", "/detokenize", "/v1/detokenize")
+_BODY_DISCARD_LIMIT = 64 * 1024              # a GET or DELETE has no use for a body: one this large closes the connection
 
 
 def _keepalive_seconds() -> float:
@@ -127,11 +128,11 @@
             except (BrokenPipeError, ConnectionResetError):          # the client has gone
                 self.close_connection = True
 
-        def _discard_body(self) -> None:
+        def _discard_body(self, limit: int = LIMIT) -> None:
             """Read a refused request's body, so it cannot reach the next request on this connection."""
 
             try:
-                read_body(self)                  # Content-Length or chunked; a refusal has closed the connection
+                read_body(self, limit=limit)     # Content-Length or chunked; a refusal has closed the connection
             except RequestError:
                 pass
 
@@ -146,6 +147,7 @@
             self.close_connection = True
 
         def do_GET(self):
+            self._discard_body(_BODY_DISCARD_LIMIT)
             route = self.path.split("?", 1)[0].rstrip("/")
             if route in ("/metrics", "/v1/metrics"):
                 return metrics.send(self, app)
@@ -160,6 +162,7 @@
                 self._json(404, {"error": "not found"})
 
         def do_DELETE(self):
+            self._discard_body(_BODY_DISCARD_LIMIT)
             responses.delete(self, app, responses.route(self.path))
 
         def do_POST(self):