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()