Repository navigation
Description
Bug Description (facts to put in your words):
- On a new HTTP/2 connection, undici sends the first request, then holds the others until it has processed the server's SETTINGS frame.
- That frame arrives within about a millisecond. But if the process is busy right then (for example a server rendering a page), the held requests leave only once it's free.
- On a connection that's already open, requests dispatched together go out together.
- It started in 8.2.0 with PR fix(http2): respect peer max concurrent streams #5135, the fix for HTTP/2 dispatch can exceed the server's advertised
SETTINGS_MAX_CONCURRENT_STREAMS#5134.
Standalone reproduction script:
'use strict'
const { test } = require('node:test')
const assert = require('node:assert')
const { once } = require('node:events')
const { Worker } = require('node:worker_threads')
const diagnosticsChannel = require('node:diagnostics_channel')
const { H2CClient } = require('undici')
const SERVER = `
const { parentPort } = require('node:worker_threads')
const { createServer } = require('node:http2')
const server = createServer()
server.on('stream', (stream, headers) => {
parentPort.postMessage({ path: headers[':path'], at: performance.timeOrigin + performance.now() })
stream.respond({ ':status': 200 })
stream.end('ok')
})
server.listen(0, '127.0.0.1', () => parentPort.postMessage({ port: server.address().port }))
`
const BUSY_MS = 50
test('a new HTTP/2 connection sends requests dispatched together at once', { timeout: 60000 }, async (t) => {
const worker = new Worker(SERVER, { eval: true })
t.after(() => worker.terminate())
const [{ port }] = await once(worker, 'message')
const arrivals = []
worker.on('message', (m) => arrivals.push(m))
const client = new H2CClient(`http://127.0.0.1:${port}`)
t.after(() => client.close())
let busyNext = false
const onSend = () => {
if (!busyNext) return
busyNext = false
setImmediate(() => {
const end = performance.now() + BUSY_MS
while (performance.now() < end);
})
}
diagnosticsChannel.subscribe('undici:client:sendHeaders', onSend)
t.after(() => diagnosticsChannel.unsubscribe('undici:client:sendHeaders', onSend))
async function pair (label) {
arrivals.length = 0
busyNext = true
await Promise.all(['/one', '/two'].map(async (path) => {
const { body } = await client.request({ path, method: 'GET' })
await body.text()
}))
await new Promise((resolve) => setTimeout(resolve, 20))
const gap = Math.abs(arrivals[1].at - arrivals[0].at)
console.log(`${label}: the second request arrived ${gap.toFixed(1)} ms after the first`)
return gap
}
const fresh = await pair('new connection')
const open = await pair('open connection')
assert.ok(open < BUSY_MS / 2, 'on an open connection both arrive together')
assert.ok(fresh < BUSY_MS / 2, `on a new connection the second arrived ${fresh.toFixed(1)} ms after the first, after the ${BUSY_MS} ms busy spell`)
})
The server runs in a worker thread so the busy spell doesn't stall it.
Expected Behavior: requests dispatched together on a new connection go out together. RFC 9113 §3.4 lets a client send right after its connection preface, and Go's client allows 100 streams before SETTINGS arrive.
Actual Behavior: the second request goes out only after the busy spell. In the script, 50 ms apart on a new connection, 0.1 ms apart on an open one.
Logs:
undici 8.12.4: new connection: the second request arrived 52.4 ms after the first (3 runs of 3)
open connection: the second request arrived 0.1 ms after the first
undici 8.1.0: new connection: the second request arrived 1.1 ms after the first
Environment:
- OS: Debian GNU/Linux 13 (trixie), in Docker on WSL2
- Node.js: v26.13.0
- undici: 8.12.4 (also seen on 8.12.3)
Additional context:
- Two places hold it in 8.12.4:
- busy() in client-h2.js line 377: session[kRemoteSettings] === false && client[kRunning] > 0.
- getMaxConcurrent() in client.js lines 93–98: client[kMaxConcurrentStreams] is unset until SETTINGS arrive, and || 1 reads that as 1.
- Lifting both (allowing 100 before SETTINGS) makes the script pass, about 1 ms apart.
- The spec:
- RFC 9113 §3.4: a client may send frames right after its preface.
- §6.5.2: MAX_CONCURRENT_STREAMS has no limit initially, and servers are recommended to allow at least 100.
- §8.7: a stream refused with REFUSED_STREAM is safe to retry.
- Go's client: initialMaxConcurrentStreams = 100 until the server's first SETTINGS; x/net/http2: max connections overflow golang/go#39389 is why it's 100 rather than unlimited.
- A possible change: before SETTINGS, allow up to the client's maxConcurrentStreams capped at 100, as Go does. HTTP/2 dispatch can exceed the server's advertised
SETTINGS_MAX_CONCURRENT_STREAMS#5134's server, which allows one stream, would refuse the extras with REFUSED_STREAM, which is safe to retry. - Real-world effect: a Next.js server's two requests to its API, sent together, reached the API 7–26 ms apart on the first page after a lull.
Activity