Repository navigation
Description
Version
undici8.7.0- Node.js
>=22.19(observed on v24.x)
Summary
In lib/dispatcher/client-h2.js, the writeH2 abort handler closes the stream to send RST_STREAM and then defers the actual util.destroy(stream) behind a setImmediate. On a healthy event loop this is fine. But if the event loop is stalled (a long synchronous task), the setImmediate callback does not run, so every aborted Http2Stream — and the whole request graph each one pins (request/response objects, header lists, buffered body chunks) — stays alive until the loop drains. Under sustained request churn with aborts (timeouts, client cancellations) during a stall, these accumulate without bound and the process OOMs.
Offending code
lib/dispatcher/client-h2.js, writeH2, abort branch:
if (state.stream != null) {
clearRequestStream(request)
// On Abort, we close the stream to send RST_STREAM frame.
const stream = state.stream
stream.close()
// close() alone leaves cleanup waiting on the 'close' event; on a busy,
// long-lived multiplexed session that event can fail to fire, leaving the
// native Http2Stream (and the whole request graph it pins) alive for the
// session's life. Destroy the stream to release the handle
// deterministically, but defer it by a setImmediate so the RST_STREAM
// frame queued by close() gets a chance to be written first.
setImmediate(() => {
if (!stream.destroyed) {
util.destroy(stream)
}
})
// ...
}The comment correctly identifies that close() alone can leak the stream (cleanup waits on a 'close' event that may never fire on a busy multiplexed session) and that a deterministic util.destroy is needed. The problem is the deferral: setImmediate moves the deterministic teardown onto the macrotask queue, which reintroduces exactly the "cleanup that might not run" hazard the destroy was added to avoid — this time gated on event-loop liveness rather than the 'close' event.
Why the deferral defeats its own purpose
setImmediatecallbacks run in the check phase, i.e. only once the current synchronous work yields and the loop advances.- A stalled loop (large JSON encode/parse, a synchronous hot path, GC pauses, or an application that periodically blocks) delays all pending
setImmediatecallbacks for the duration of the stall. - Every request aborted during the stall queues one more deferred-destroy closure that captures
stream. None of them run, so the nativeHttp2Streamhandles and the objects they retain pile up. - The stall and the abort rate are often correlated (backpressure/timeouts spike precisely when the loop is busy), so this is a positive-feedback leak, not a bounded overhead.
Impact (real-world)
We hit this in production on a high-throughput fetch worker (~100K req/s across the fleet) that periodically stalls its event loop for tens of seconds under bursty downstream work. After upgrading 8.5.0 → 8.7.0, worker memory tripled (steady ~4.4 GB → ~11–12 GB, tail pinned at the 16 GiB cgroup limit) and the fleet entered a sustained OOMKilled crashloop (0 restarts/week → 120–240 restarts/hr). We were previously carrying a local patch that destroyed the stream synchronously; the OOMs began the moment we dropped it in favor of stock 8.7.0.
Reproduction (sketch)
- Start an HTTP/2 server whose responses are slow/never-ending (so client streams stay open).
- From an undici
Client/PoolwithallowH2: true, dispatch a batch of requests, each with anAbortSignal, then abort them. - Immediately block the event loop synchronously (e.g.
const end = Date.now() + 30_000; while (Date.now() < end) {}), simulating a stall. - Observe that the aborted streams are not destroyed during the block —
process._getActiveHandles()/ RSS stays elevated and theHttp2Streams remain — and are only torn down after the block ends and thesetImmediatequeue drains. Repeating steps 2–3 under load grows memory monotonically.
(The synchronous while loop is a stand-in for any real workload that starves the check phase.)
Expected vs actual
- Expected: an aborted request's stream is torn down deterministically and promptly, independent of event-loop liveness, so memory does not grow with the number of aborts issued during a stall.
- Actual: teardown is deferred to
setImmediate; a stalled loop postpones all deferrals and aborted streams accumulate until OOM.
Suggested fix
Destroy the stream synchronously in the abort path (drop the setImmediate wrapper):
stream.close()
if (!stream.destroyed) {
util.destroy(stream)
}This is the behavior prior to the deferral and does not depend on the loop advancing.
If the setImmediate was added specifically to let the RST_STREAM frame queued by close() flush before the handle is destroyed, that ordering concern is worth confirming: in practice stream.close(code) queues RST_STREAM and a subsequent synchronous stream.destroy() still emits it, so the deferral may not be necessary. If some ordering guarantee is required, it should be achieved without making deterministic resource cleanup contingent on the event loop being free (e.g. tie teardown to the close/error events with a synchronous fallback, rather than an unconditional setImmediate).
Workaround
Pin a patch that restores the synchronous destroy in writeH2's abort branch.
Activity
staylor commentedon Jul 14, 2026
#5559
fix(h2): complete request stream on 'end' instead of waiting for 'close'
fix(h2): complete request stream on 'end' instead of waiting for 'close'
fix(h2): complete request stream on 'end' instead of waiting for 'close'
fix(h2): complete request stream on 'end' instead of waiting for 'close'