From 03b08bc0cef1b6127184c8f61c2aa9a2f3c39ca1 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 30 Nov 2026 13:31:23 +0000 Subject: [PATCH] Serve one request per loopback connection (gunicorn keepalive = 0) Since the gateway moved from Werkzeug to gunicorn on 2026-09-11, about 0.8% of streamed responses have been cut off mid-body with no error event. chat-api sees "peer closed connection without sending complete message body", and the user sees a turn end before any text. For the two weeks before, under Werkzeug, it was 0.03%. The rate is the same on all six enclaves and does not rise with load. Streams that break do so while data is flowing, not after long silences, so this is not an idle timeout. The difference on that hop: Werkzeug closed the loopback connection after every response, and gunicorn kept it alive for an hour so nitriding's Go proxy could reuse it. With keepalive = 0, gunicorn answers every request with Connection: close and the proxy opens a fresh loopback connection per request, as under Werkzeug. That costs a fraction of a millisecond; chat-api's TLS connection to nitriding is separate and stays alive. Because the server announces the close, the silent-idle-close race (a bare 502, since the proxy does not retry a written POST) cannot happen. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SQvDLzSf3RR7U4hG3QQD24 --- CLAUDE.md | 13 +++++++++---- tee_gateway/gunicorn_conf.py | 24 +++++++++++++++++------- tee_gateway/test/test_gunicorn_conf.py | 5 +++++ 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 02cf409..7f5bb42 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -345,10 +345,15 @@ tunables: a new signing key and no provider keys and keep answering requests wrongly. Dying is what the bare process did; keep it that way. `scripts/start.sh` `exec`s gunicorn so that status is the container's. -- **`keepalive = 3600`.** nitriding's Go proxy pools idle loopback connections - with no idle timeout and does not retry a POST it has written; the server - must not be the side that closes an idle connection first (that race is a - bare 502 to the relay). +- **`keepalive = 0`.** One request per loopback connection: every response + says `Connection: close`, so nitriding's Go proxy never pools it. With + `keepalive = 3600` (from the gunicorn switch, 2026-09-11) about 0.8% of + streams were cut off mid-body with no error event (chat-api's "TEE OHTTP + stream broke mid-response"); under Werkzeug, which closed after every + response, it was 0.03%. Don't turn keep-alive back on without watching + `chatapi.ohttp.stream.upstream_broken`. Never let the server close a pooled + connection *silently* either: the proxy does not retry a POST it has + written, so that race is a bare 502. - **`timeout = 0`.** The heartbeat watchdog is off because, with worker exit fatal, a false positive under load would take the enclave down. - `GUNICORN_THREADS` caps concurrent requests (each chat stream holds a diff --git a/tee_gateway/gunicorn_conf.py b/tee_gateway/gunicorn_conf.py index 59ebb7f..e7d36d0 100644 --- a/tee_gateway/gunicorn_conf.py +++ b/tee_gateway/gunicorn_conf.py @@ -25,10 +25,20 @@ what happens next. Connections to this server come only from nitriding's Go reverse proxy on -loopback. Go's transport pools idle connections indefinitely (nitriding sets -no IdleConnTimeout) and does not retry a POST it has already written, so the -server must never be the side that closes an idle connection first; hence a -keep-alive far longer than any realistic idle gap. +loopback, and each one carries a single request (``keepalive = 0``): every +response says ``Connection: close``, so the proxy never pools it, exactly as +under Werkzeug. The switch to gunicorn originally kept loopback connections +alive for an hour so the proxy could reuse them. From that deploy on, about +0.8% of streamed responses were cut off mid-body with no error event: chat-api +saw ``peer closed connection without sending complete message body``, and the +browser saw its stream drop before any text. Before it, under Werkzeug's +close-every-response behaviour, the rate was 0.03%. A fresh loopback +connection costs a fraction of a millisecond; chat-api's TLS connection to +nitriding is separate and stays alive. + +Because the server announces the close, there is none of the race that +silently closing an idle pooled connection would cause (the proxy does not +retry a POST it has already written, so that race is a bare 502). """ from __future__ import annotations @@ -50,9 +60,9 @@ worker_connections = 1000 backlog = 2048 -# Never let this server close an idle keep-alive connection before the Go -# proxy does (see module docstring); an hour is "never" for loopback traffic. -keepalive = 3600 +# One request per loopback connection (see module docstring): gunicorn then +# sends `Connection: close` and the proxy does not reuse the connection. +keepalive = 0 # The gthread worker heartbeats from its main loop, so a long-running request # cannot trip this — but a GIL-starved main loop under heavy load could, and diff --git a/tee_gateway/test/test_gunicorn_conf.py b/tee_gateway/test/test_gunicorn_conf.py index bff9183..4ebc68c 100644 --- a/tee_gateway/test/test_gunicorn_conf.py +++ b/tee_gateway/test/test_gunicorn_conf.py @@ -34,6 +34,11 @@ def test_one_gthread_worker(self): self.assertEqual(0, gunicorn_conf.max_requests) self.assertFalse(getattr(gunicorn_conf, "preload_app", False)) + def test_one_request_per_loopback_connection(self): + # Keep-alive let nitriding reuse loopback connections, and ~0.8% of + # streams were then cut off mid-body (see the module docstring). + self.assertEqual(0, gunicorn_conf.keepalive) + if __name__ == "__main__": unittest.main()