Skip to content

http: reject pipelined protocol switches - #66676

Open
mcollina wants to merge 1 commit into
nodejs:mainfrom
mcollina:fix-http-pipelined-upgrade-order
Open

mcollina wants to merge 1 commit into
nodejs:mainfrom
mcollina:fix-http-pipelined-upgrade-order

Conversation

@mcollina

Copy link
Copy Markdown
Member

Destroy the connection instead of emitting the upgrade or connect event when an earlier response is still pending. This prevents protocol-switch responses from being written ahead of earlier HTTP responses.

Document the behavior and cover both methods with regression tests.

—

AI gen, reviewed by me.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/http
  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added http Issues and PRs related to the http subsystem. needs-ci PRs that need a full CI run. labels Oct 12, 2026
@codecov

codecov Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.72%. Comparing base (5b5f384) to head (1d9ce1d).
⚠️ Report is 241 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #66676      +/-   ##
==========================================
+ Coverage   90.69%   90.72%   +0.02%     
==========================================
  Files         797      801       +4     
  Lines      278883   280738    +1855     
  Branches    53632    54182     +550     
==========================================
+ Hits       252940   254698    +1758     
+ Misses      16304    16207      -97     
- Partials     9639     9833     +194     
Files with missing lines Coverage Δ
lib/internal/http/server.js 96.68% <100.00%> (+0.04%) ⬆️

... and 233 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mcollina

Copy link
Copy Markdown
Member Author

@copilot resolve the merge conflicts on this branch.

Comment thread lib/_http_server.js Outdated

// Switching protocols while an earlier response is still pending would
// allow the upgrade response to be written out of order.
if (socket._httpMessage || state.outgoing.length > 0) {

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.

_httpMessage is unset until finish, but that fires async, not immediately. It doesn't actually get unset until a tick after the write call.

If the event loop blocks around the same time (event loop or just heavy GC) then finish can fire quite a long time after the response has been sent to the client. If it blocks longer than RTT (especially for localhost/LAN) then you can get a new request received in the meantime, and in that case it's possible for this to fire and kill connections in normal scenarios, where there's no pipelining present at all.

In practice I think that means a very busy server + low RTT to the client, so quite niche, but in that case you'd hit spurious upgrade connection failures at random, without any weird pipelining involved, which sounds very annoying for everybody.

You can check socket._httpMessage?.finished === false safely instead I think. That's set to true synchronously on end(), so it does exactly the same thing with no race involved:

Suggested change
if (socket._httpMessage || state.outgoing.length > 0) {
if (socket._httpMessage?.finished === false || state.outgoing.length > 0) {

@mcollina mcollina added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 16, 2026
Destroy the connection instead of emitting the upgrade or connect event
when an earlier response is still pending. This prevents protocol-switch
responses from being written ahead of earlier HTTP responses.

A response that has ended but has not emitted 'finish' yet is already
queued on the socket, so it does not block the switch. When its 'finish'
fires after the switch, the keep-alive timeout is not armed on the
switched socket.

Document the behavior and cover both methods with regression tests.

Assisted-by: Codex
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollina
mcollina force-pushed the fix-http-pipelined-upgrade-order branch from dac57a0 to 1d9ce1d Compare November 18, 2026 10:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

http Issues and PRs related to the http subsystem. needs-ci PRs that need a full CI run. request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants