Skip to content

server(sse): stop sending Connection: keep-alive on the streaming endpoints - #426

Open
jamesburton wants to merge 1 commit into
kkokosa:mainfrom
jamesburton:issue/422-sse-connection-header
Open

server(sse): stop sending Connection: keep-alive on the streaming endpoints#426
jamesburton wants to merge 1 commit into
kkokosa:mainfrom
jamesburton:issue/422-sse-connection-header

Conversation

@jamesburton

Copy link
Copy Markdown

Connection is a hop-by-hop, connection-specific header field. RFC 9113 §8.2.2 forbids it over
HTTP/2 and HTTP/3 (an endpoint MUST treat a message containing connection-specific header fields as
malformed), and over HTTP/1.1 it is merely redundant since persistent connections are the default.
Both OpenAI-compatible streaming endpoints were setting it.

Kestrel filters it on HTTP/2, so nothing observable breaks in the common configuration — which is
why it went unnoticed. It matters when a strict intermediary is in the path, and it reads as
intentional to anyone copying the pattern into a new endpoint, which is how it reached the Anthropic
endpoint in #326.

Changes

  • ChatCompletionEndpoint.HandleStreamingAsync and CompletionEndpoint.HandleStreamingAsync no
    longer set Connection.
  • Both now configure the response through a new shared internal SseResponse.ApplyHeaders, which
    sets the two headers SSE actually needs (text/event-stream, Cache-Control: no-cache) and
    carries the note on why Connection is deliberately absent. One place for it to not be re-added,
    rather than a comment repeated per endpoint.
  • tests/DotLLM.Tests.Unit/Server/SseResponseHeaderTests.cs — regression guard asserting the header
    is absent and the other two are set, in the pattern used by server: Anthropic-compatible Messages API (/v1/messages) (#325) #326's AnthropicStreamingTests.
    Written self-contained since that branch is not merged.

Verification

dotnet test tests/DotLLM.Tests.Unit --filter "FullyQualifiedName~Tests.Unit.Server" -v q --nologo
Passed! - Failed: 0, Passed: 20, Skipped: 0, Total: 20

Closes #422

…points (#422)

Connection is a hop-by-hop, connection-specific header field: RFC 9113 8.2.2
forbids it over HTTP/2 and HTTP/3, and it is redundant over HTTP/1.1 where
persistent connections are already the default. Kestrel filters it on HTTP/2,
so nothing observable breaks today, but a strict intermediary in the path need
not be as forgiving, and its presence reads as intentional to anyone copying
the pattern into a new endpoint.

Both OpenAI-compatible streaming endpoints now configure their response through
a shared internal SseResponse.ApplyHeaders, which sets the two headers SSE
actually needs (text/event-stream and Cache-Control: no-cache) and documents
why Connection is deliberately absent, so it has one place to not be re-added.

Adds SseResponseHeaderTests as the regression guard.
Copilot AI review requested due to automatic review settings July 31, 2026 10:30

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Removes the illegal/redundant Connection: keep-alive header from the OpenAI-compatible SSE streaming endpoints to avoid malformed responses over HTTP/2+ (and reduce the chance the pattern gets copied into new endpoints). Consolidates SSE header setup into a single shared helper and adds a regression test.

Changes:

  • Removed Connection header emission from /v1/chat/completions and /v1/completions streaming responses.
  • Introduced SseResponse.ApplyHeaders(HttpContext) as the single shared place to apply SSE-required headers (Content-Type, Cache-Control) and document why Connection must not be set.
  • Added a unit test guarding the expected SSE headers and asserting Connection is absent.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
src/DotLLM.Server/Endpoints/ChatCompletionEndpoint.cs Routes streaming response header setup through SseResponse.ApplyHeaders and stops setting Connection.
src/DotLLM.Server/Endpoints/CompletionEndpoint.cs Routes streaming response header setup through SseResponse.ApplyHeaders and stops setting Connection.
src/DotLLM.Server/Endpoints/SseResponse.cs New shared internal helper that applies SSE headers and documents the Connection header prohibition.
tests/DotLLM.Tests.Unit/Server/SseResponseHeaderTests.cs Regression tests asserting required SSE headers are set and Connection is not emitted.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

server(sse): Connection: keep-alive is sent on the OpenAI streaming endpoints (prohibited over HTTP/2+)

2 participants