Skip to content

Close keep-alive connections left with an unread request body - #71

Merged
ghosthack merged 2 commits into
masterfrom
cursor/unread-body-keepalive-0553
Nov 28, 2026
Merged

ghosthack merged 2 commits into
masterfrom
cursor/unread-body-keepalive-0553

Conversation

@ghosthack

Copy link
Copy Markdown
Owner

Bug

HTTP/1.1 keep-alive is the default. When a handler never reads the request body — a 413 because Content-Length is over the form limit, a 405 or 404, or a success path that ignores the body — that body stays on the socket. The JDK server only discards about 64KB on the way out and will not read the next request until the declared Content-Length has arrived. A pipelined follow-up is swallowed, and the connection stays open until the rest of the body shows up or the idle timeout fires.

Fix

On the way out of Server.handle / HttpContext.finish, if the request body was not read through to EOF:

  • A declared length small enough to discard safely (64KB, the same bound the JDK uses) is read and thrown away, so the connection can still carry the next request. The discard stops at the declared length, so a pipelined follow-up is left intact.
  • Anything larger, including a chunked body, is not waited on. The response is sent with Connection: close and the socket is dropped, so the exchange is not left on the keep-alive path.

The same rule applies to 413, 405, 404, and successful handlers that ignore the body. A body the handler already read through to EOF is unchanged and can stay keep-alive.

Tests

A socket client pipelines a follow-up GET after those cases (ServerKeepAliveTest). ./mvnw test — 373 tests, 0 failures.

Open in Web Open in Cursor 

cursoragent and others added 2 commits November 27, 2026 20:50
The JDK server discards only about 64KB of an unread body and then
waits until Content-Length is satisfied, so a pipelined follow-up on
that socket is never answered. Discard a small declared remainder so
the connection can still be reused; otherwise send Connection: close
and drop the socket without waiting for the rest of the body.

Co-authored-by: ghosthack <ghosthack@users.noreply.github.com>
Interrupting the handler before the response stream closed reset the
socket on Java 25 and discarded the reply the client had not read yet.
Abandon the unread-body drain only after it blocks, which is the same
path the empty and streaming responses already use.

Co-authored-by: ghosthack <ghosthack@users.noreply.github.com>
@ghosthack
ghosthack merged commit f9c64cb into master Nov 28, 2026
6 checks passed
@ghosthack
ghosthack deleted the cursor/unread-body-keepalive-0553 branch November 28, 2026 01:02
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.

2 participants