Skip to content

Serve one request per loopback connection (gunicorn keepalive = 0) - #204

Merged
adambalogh merged 1 commit into
mainfrom
og/awesome-bell-lnt5d2
Dec 1, 2026
Merged

adambalogh merged 1 commit into
mainfrom
og/awesome-bell-lnt5d2

Conversation

@adambalogh

Copy link
Copy Markdown
Contributor

Why

Since the gateway moved from Werkzeug to gunicorn in #163 (2026-09-11), about 0.8% of streamed responses have been cut off mid-body with no error event. chat-api logs httpx.RemoteProtocolError: peer closed connection without sending complete message body. Users see a chat turn end with nothing in it, or a dropped stream.

chat-api's stream_incomplete_or_upstream_error at status_code:200, per week:

Week of Broken / streams Rate
Aug 28 9 / 29.1k 0.03%
Sep 4 11 / 28.4k 0.04%
Sep 11 (gunicorn) 568 / 72.5k 0.78%
Sep 18 – Nov 27 0.65–1.1%

What the new break logging (OpenGradient/chat-api#526) shows over the last week:

  • Every enclave: all six enclaves sit at 0.72–0.85%.
  • Not load: 0.83% in the quietest hours vs 0.78% in the busiest.
  • Not an idle timeout: streams that break have a typical longest silence of ~6s (grok-4.3 ~2s), while streams that finish survive silences of up to 13 minutes.
  • Not the app: the gateway's own code always sends an error event when something fails, so a clean close without one comes from the layer below it.

The obvious change on that hop: Werkzeug sent Connection: close on every response, so each request got a fresh loopback connection. gunicorn kept connections alive for an hour so nitriding's Go proxy could reuse them.

What

keepalive = 0: gunicorn answers every request with Connection: close, and nitriding opens a fresh loopback connection per request, as under Werkzeug.

  • Cost: a loopback TCP handshake inside the enclave, a fraction of a millisecond. chat-api's TLS connection to nitriding is separate and stays alive.
  • No 502 race: the old comment warned about the server closing a pooled connection silently, which is a bare 502 because the proxy doesn't retry a POST it has written. That can't happen when the server announces the close, so the proxy never pools the connection in the first place.
  • Docs: the module docstring and the CLAUDE.md "Production server" section now say why, and to watch chatapi.ohttp.stream.upstream_broken before turning keep-alive back on.

#163 also added body_preread and the new error payloads in the same deploy. Connection reuse is the likeliest of the three, but not proven. This change is the test: if chatapi.ohttp.stream.upstream_broken falls back toward 0.03% after deploy, that's confirmed. If it doesn't, the next suspect is an exception escaping the OHTTP stream wrapper after headers are sent, which gunicorn handles by closing the connection the same way ("Error handling request").

Testing

  • Live check: ran gunicorn with this config and a streaming WSGI app. The response is chunked and carries Connection: close.
  • New test: test_one_request_per_loopback_connection pins the setting.
  • Checks: ruff format --check, ruff check, mypy tee_gateway; the CI pytest set passed (550 passed, 6 skipped).
  • PCRs: this changes them (the config file is in the image); uv.lock is untouched. measurements.txt was not regenerated, since there's no enclave build here.

Generated by Claude Code

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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SQvDLzSf3RR7U4hG3QQD24
@adambalogh
adambalogh marked this pull request as ready for review November 30, 2026 13:32
Copilot AI balanced review requested due to automatic review settings November 30, 2026 13:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@adambalogh
adambalogh merged commit 4876021 into main Dec 1, 2026
9 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants