Skip to content

fix: drain unconsumed request bodies so keep-alive connections don't hang - #16170

Merged
Rich-Harris merged 2 commits into
sveltejs:version-3from
mcmxcdev:fix/drain-unconsumed-request-body
Jul 2, 2026
Merged

Rich-Harris merged 2 commits into
sveltejs:version-3from
mcmxcdev:fix/drain-unconsumed-request-body

Conversation

@mcmxcdev

@mcmxcdev mcmxcdev commented Jun 25, 2026 •

Copy link
Copy Markdown
Contributor

closes #14916
closes #15526

Problem

When a Node route doesn't read the request body (e.g. a page route receiving a POST), the unread bytes stay buffered in the socket. On keep-alive connections Node's HTTP parser reads those leftover bytes as the next request, fails to parse them, and resets the connection — losing any pipelined request.

Because get_raw_body attaches a data listener, Node marks the request as consumed (req._consuming) and skips its own automatic drain, so we have to do it ourselves.

Fix

After the response is sent (finish/close), drain_request removes the lingering data listener and calls req.resume() to discard the remaining bytes, keeping the connection usable. The end/error listeners stay attached so the body's ReadableStream is still closed/errored, and a consumer that stopped reading mid-body sees a clean end instead of hanging.

Tests

Added unit tests in packages/kit/src/exports/node/index.spec.js covering:

  • draining an unconsumed body (content-length and chunked)

  • draining the remainder of a partially consumed body

  • closing the body stream after draining

  • Tests added

  • Changeset added (patch)

Edits

  • Allow edits from maintainers

…hang

When a Node route doesn't read the request body (e.g. a page route
receiving a POST), the unread bytes stay buffered in the socket. On
keep-alive connections Node's HTTP parser reads those leftover bytes as
the next request, fails to parse them, and resets the connection. Resume
the request after the response is sent to discard the bytes and keep the
connection usable.

Closes sveltejs#14916
Closes sveltejs#15526

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@changeset-bot

changeset-bot Bot commented Jun 25, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 9141e4e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@elliott-with-the-longest-name-on-github

Copy link
Copy Markdown
Contributor

I think this PR may need to target version-3 -- we've made some significant updates to adapter-node on that side of things and I really don't want to have to de-conflict them again

@mcmxcdev
mcmxcdev changed the base branch from main to version-3 June 30, 2026 18:49
@mcmxcdev

ghost commented Jun 30, 2026

Copy link
Copy Markdown
Contributor Author

I think this PR may need to target version-3 -- we've made some significant updates to adapter-node on that side of things and I really don't want to have to de-conflict them again

Changed target branch from main to version-3, luckily no merge conflicts ;)

@svelte-docs-bot

ghost commented Jun 30, 2026

Copy link
Copy Markdown

ghost left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

4 participants