Pull requests / #715

#715 serve: say Connection: close on every response

closed · @evaanp · 0 comentários · No GitHub

Server & APILinux

Descrição

## What

The server speaks HTTP/1.0 and closes the connection after every response, but it never said so: no `Connection`
header. A client that pools connections can then hand the socket back to its pool and put its next request on it
before the server's close has reached it. That request fails with "the response ended prematurely", and the
server never reads it, so nothing shows in its log.

`end_headers` now adds `Connection: close` to every response (JSON, SSE, 204), which tells a pooling client not to
reuse the socket.

## Measured

On our client, an agent app on .NET 10 (`SocketsHttpHandler`) with several background tasks queued behind one
another: 25 of ~1,000 requests in one night failed this way, each 1-24 ms after the previous response had
completed, and none of them reached the engine. Reproduced with a server in the same style (`BaseHTTPRequestHandler`,
HTTP/1.0) that delays its close by 10 ms to stand in for the network: 200 of 400 back-to-back requests failed
without the header, 0 with it.

Two tests (`ConnectionClose`: a non-streamed and a streamed answer carry the header); both fail without the change.
`python -m unittest serve.test_server`: 120 passed.

Developed with an AI coding assistant; measured on Linux, a .NET 10 client against the server on a Ryzen 7 9700X.

No site

Links install, modelos, releases.