Skip to content

fix(pool): propagate useH2c to connector when connections > 1#5031

Merged
mcollina merged 1 commit intonodejs:mainfrom
SAY-5:fix-pool-useh2c
Apr 17, 2026
Merged

fix(pool): propagate useH2c to connector when connections > 1#5031
mcollina merged 1 commit intonodejs:mainfrom
SAY-5:fix-pool-useh2c

Conversation

@SAY-5
Copy link
Copy Markdown
Contributor

@SAY-5 SAY-5 commented Apr 15, 2026

Fixes #4737.

When constructing a Pool directly (or indirectly via Agent with connections > 1), the useH2c option was not in the constructor's destructured parameter list. It ended up in ...options and was therefore not forwarded to buildConnector. The resulting connector did not set socket.alpnProtocol = 'h2' for plaintext h2c, so child Clients picked the HTTP/1 context for the new socket. The first response carrying an HTTP/2 SETTINGS frame then failed parsing with:

HTTPParserError: Response does not match the HTTP/1.1 protocol (Expected HTTP/, RTSP/ or ICE/)

connections === 1 was unaffected because Agent's default factory short-circuits to new Client(origin, opts), and Client already destructures and forwards useH2c to buildConnector.

The fix adds useH2c to Pool's destructured options, passes it to buildConnector, and stores it on kOptions for symmetry with allowH2.

I added two regression tests in test/h2c-client.js covering both Pool and Agent with useH2c: true, connections: 2 against an http2.createServer() (h2c) backend. Both tests fail on main with the parser error and pass with the fix.

Test results:

  • test/h2c-client.js: 10/10 pass (8 existing + 2 new)
  • test/pool.js: 35/35 pass
  • test/http2-dispatcher.js: 11/11 pass
  • test/http2-agent.js, test/http2-alpn.js: pass
  • eslint lib/dispatcher/pool.js test/h2c-client.js: clean

Manually verified the original repro from the issue (Promise.all of 3 requests through new Agent({ useH2c: true, connections: 2 })) now succeeds.

When constructing a Pool, the useH2c option was not destructured from the
constructor options. As a result, when Pool built its own connector via
buildConnector, useH2c was lost and the resulting socket never had its
alpnProtocol set to 'h2'. Child Clients then received an h2c connector
that produced h1 sockets, causing HTTPParserError on the first response
that contained an HTTP/2 SETTINGS frame.

This only affected Agent/Pool with connections > 1 because connections === 1
short-circuits to a Client, which already destructures and forwards useH2c.

Fixes nodejs#4737
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.03%. Comparing base (0860142) to head (165a84e).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5031   +/-   ##
=======================================
  Coverage   93.02%   93.03%           
=======================================
  Files         110      110           
  Lines       35793    35795    +2     
=======================================
+ Hits        33298    33303    +5     
+ Misses       2495     2492    -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 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.

@SAY-5
Copy link
Copy Markdown
Contributor Author

SAY-5 commented Apr 16, 2026

@metcoder95 thanks for approving. Undici CI is green and the fix is small. Would you mind merging when you get a chance?

@SAY-5
Copy link
Copy Markdown
Contributor Author

SAY-5 commented Apr 16, 2026

@metcoder95 gentle ping — mergeable state is clean, all CI green, approved. Would you mind merging when convenient?

Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina mcollina merged commit 8d1e8d9 into nodejs:main Apr 17, 2026
35 checks passed
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.

useH2c with connections > 1 causes HTTPParserError

4 participants