Repository navigation
fix(http2): dispatch requests before SETTINGS up to the recommended 100 - #6203
MIbtehajNasar wants to merge 3 commits into
Conversation
On a new HTTP/2 connection undici sent the first request and then held the rest until the server's SETTINGS frame was processed. When the event loop was busy right after that first request (for example a server rendering a page), the held requests left only once it freed up, adding latency that was not present before nodejs#5135. Initialize the per-connection stream ceiling to min(maxConcurrentStreams, 100) before SETTINGS arrive, as RFC 9113 section 6.5.2 recommends and Go's client does, and drop the busy() guard that allowed only one in-flight request until SETTINGS. A server advertising a lower limit refuses the extra streams with REFUSED_STREAM, which section 8.7 permits retrying and undici already does. The existing queue-above-limit test asserted server-side arrival order; since refused streams are now retried and HTTP/2 gives no cross-stream ordering guarantee, it asserts on the set of paths instead. Fixes: nodejs#6202
| // limit refuses the extra streams with REFUSED_STREAM, which section 8.7 | ||
| // permits retrying. onHttp2RemoteSettings replaces this with the confirmed | ||
| // limit once SETTINGS arrive. | ||
| client[kMaxConcurrentStreams] = Math.min(client[kHTTP2Options].maxConcurrentStreams, 100) |
There was a problem hiding this comment.
I think this should be documented somewhere
There was a problem hiding this comment.
🟡 Changes recommended
Dispatching a streaming POST before peer SETTINGS can exceed the server’s stream limit and trigger REFUSED_STREAM, causing uploads to fail because their bodies cannot be replayed.
1 open issue
What changed in this PR
This PR addresses #6202 by allowing Undici’s HTTP/2 clients to dispatch multiple requests on a fresh connection before receiving server SETTINGS, reducing startup delays when the event loop is busy.
Changes:
- Initialize the pre-SETTINGS stream ceiling to the configured limit capped at 100, and remove the single-request gate.
- Replace the optimistic ceiling with the server-confirmed limit when SETTINGS arrive.
- Add a worker-thread timing regression test and make the existing low-limit test independent of request arrival order.
| File | Description |
|---|---|
test/h2c-client.js |
Adds fresh-connection timing coverage and adjusts low-limit path assertions. |
lib/dispatcher/client-h2.js |
Enables optimistic dispatch and distinguishes initial from confirmed stream limits. |
Review effort: Balanced
| * @param {import('../core/request.js')} request | ||
| * @returns {boolean} | ||
| */ | ||
| busy (request) { |
There was a problem hiding this comment.
Agreed, addressed in 1cd9723: before SETTINGS, a request whose body can't be replayed (or that was already retried once) now waits while another request is in flight, so a REFUSED_STREAM can't fail it. The new "Should not dispatch a non-replayable body before SETTINGS while a request is running" test covers a streaming POST against maxConcurrentStreams: 1 and fails without the change.
…INGS Before the server's SETTINGS arrive, a request dispatched alongside a running one may be refused with REFUSED_STREAM by a server with a lower limit, and is only retried if its body can be replayed and it hasn't been retried already. Keep such requests waiting for SETTINGS while another request is running, and keep reporting the client busy to a Pool while it has queued work, so the pool's backpressure is unchanged before SETTINGS. Document the pre-SETTINGS behavior of maxConcurrentStreams.
|
Thanks! Pushed an update:
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #6203 +/- ##
=======================================
Coverage 91.72% 91.72%
=======================================
Files 113 113
Lines 44835 44858 +23
=======================================
+ Hits 41124 41148 +24
+ Misses 3711 3710 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|

Fixes #6202
Problem
On a new HTTP/2 connection, undici sends the first request and then holds the rest until it has processed the server's SETTINGS frame. That frame usually arrives within about a millisecond, but if the process is busy right then (for example a server rendering a page), the held requests only leave once it is free. On an already-open connection, requests dispatched together go out together.
This started in 8.2.0 with #5135 (the fix for #5134). Two things held the extra requests until SETTINGS:
busy()returnedtruewheneverkRemoteSettings === false && kRunning > 0, allowing only one in-flight request.client[kMaxConcurrentStreams]was unset until SETTINGS arrived, so the dispatch ceiling read as 1.Fix
Initialize the per-connection stream ceiling to
min(maxConcurrentStreams, 100)when the session is created, before SETTINGS arrive, and drop thebusy()guard that allowed only one in-flight request until SETTINGS.This matches the spec and other clients:
SETTINGS_MAX_CONCURRENT_STREAMShas no limit initially, and servers are recommended to allow at least 100.REFUSED_STREAMis safe to retry, which undici already does.A server that advertises a lower limit (the #5134 case,
maxConcurrentStreams: 1) refuses the extra optimistic streams withREFUSED_STREAM; undici retries them once SETTINGS have confirmed the real limit, so they never exceed it.onHttp2RemoteSettingsstill replaces the optimistic ceiling with the confirmed value, and only falls back to a previously confirmed value (not the optimistic placeholder) on a later SETTINGS frame.Tests
test/h2c-client.jsreproducing HTTP/2: requests dispatched together on a new connection are held until the server's SETTINGS frame is read #6202: with the server in a worker thread and the main thread briefly busy right after the first request, two requests dispatched together on a fresh connection now arrive together. It fails onmainand passes with this change.SETTINGS_MAX_CONCURRENT_STREAMS#5134) still passes: the server's limit is never exceeded and all requests succeed. Because refused streams are now retried and HTTP/2 gives no cross-stream ordering guarantee, it asserts on the set of received paths rather than their server-side arrival order.