Skip to content

fix: native WebSocket over H2 server after undici import#4990

Open
mcollina wants to merge 2 commits intomainfrom
fix/issue-4989-native-websocket-h2-fallback
Open

fix: native WebSocket over H2 server after undici import#4990
mcollina wants to merge 2 commits intomainfrom
fix/issue-4989-native-websocket-h2-fallback

Conversation

@mcollina
Copy link
Copy Markdown
Member

@mcollina mcollina commented Apr 7, 2026

Importing undici v8 overwrites the legacy global dispatcher
(Symbol.for('undici.globalDispatcher.1')) used by Node.js's bundled
undici. The new Agent defaults to allowH2: true, causing ALPN to
negotiate h2. Undici v8's fetch has a fallback
(dispatchWithProtocolPreference) that retries with HTTP/1.1 when
Extended CONNECT is unavailable, but the bundled undici does not.

Fix Dispatcher1Wrapper.dispatch() to force allowH2: false for legacy consumers that cannot handle the H2 WebSocket fallback.

Fixes: #4989

Importing undici v8 overwrites the legacy global dispatcher
(Symbol.for('undici.globalDispatcher.1')) used by Node.js's bundled
undici. The new Agent defaults to allowH2: true, causing ALPN to
negotiate h2. Undici v8's fetch has a fallback
(dispatchWithProtocolPreference) that retries with HTTP/1.1 when
Extended CONNECT is unavailable, but the bundled undici does not.

Fix Dispatcher1Wrapper.dispatch() to force allowH2: false for upgrade
requests, since legacy consumers cannot handle the H2 WebSocket
fallback.

Fixes: #4989
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollina
Copy link
Copy Markdown
Member Author

mcollina commented Apr 7, 2026

@cristianmadularu please test this

@mcollina mcollina requested a review from metcoder95 April 7, 2026 16:00
@cristianmadularu
Copy link
Copy Markdown

@cristianmadularu please test this

Is it released? If not, how could I reference it from my packages.json?

Copy link
Copy Markdown
Member

@KhafraDev KhafraDev left a comment

Choose a reason for hiding this comment

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

Test is failing and it'll only work on node version that bundle undici v7.

Legacy (v1) consumers (like Node.js's bundled undici on Node 22)
do not support HTTP/2. Force allowH2: false in Dispatcher1Wrapper
so that the v1 global dispatcher always uses HTTP/1.1.

Also add NODE_TLS_REJECT_UNAUTHORIZED for the regression test
since native WebSocket uses the bundled dispatcher without
rejectUnauthorized override.

Signed-off-by: Matteo Collina <hello@matteocollina.com>
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.87%. Comparing base (ce45abf) to head (2569829).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4990      +/-   ##
==========================================
- Coverage   92.94%   92.87%   -0.07%     
==========================================
  Files         110      110              
  Lines       35773    35794      +21     
==========================================
- Hits        33249    33244       -5     
- Misses       2524     2550      +26     

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

@mcollina
Copy link
Copy Markdown
Member Author

mcollina commented Apr 7, 2026

@KhafraDev ptal

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.

Undici v8.x breaks Node.js native WebSocket over WSS (HTTP/2)

5 participants