Repository navigation
Serve one request per loopback connection (gunicorn keepalive = 0) - #204
Merged
Merged
Conversation
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
Contributor
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_erroratstatus_code:200, per week:What the new break logging (OpenGradient/chat-api#526) shows over the last week:
The obvious change on that hop: Werkzeug sent
Connection: closeon 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 withConnection: close, and nitriding opens a fresh loopback connection per request, as under Werkzeug.chatapi.ohttp.stream.upstream_brokenbefore turning keep-alive back on.#163 also added
body_prereadand the new error payloads in the same deploy. Connection reuse is the likeliest of the three, but not proven. This change is the test: ifchatapi.ohttp.stream.upstream_brokenfalls 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
Connection: close.test_one_request_per_loopback_connectionpins the setting.ruff format --check,ruff check,mypy tee_gateway; the CI pytest set passed (550 passed, 6 skipped).uv.lockis untouched.measurements.txtwas not regenerated, since there's no enclave build here.Generated by Claude Code