From e1a95ab35ff29191518494b219dc4f0d04416a0a Mon Sep 17 00:00:00 2001 From: gdemesa Date: Thu, 12 Nov 2026 10:06:34 -0800 Subject: [PATCH 1/2] fix(h2): reset aborted streams with CANCEL abort() closed the stream with the default NO_ERROR code and then destroyed it synchronously. When the request body is still queued, Node defers a NO_ERROR RST_STREAM until the body has been written, and the synchronous destroy means that never happens, so the peer is never told the stream is gone. Close with NGHTTP2_CANCEL, the code for a stream that is no longer needed, which Node submits immediately. Co-Authored-By: Claude Opus 5.5 Signed-off-by: gdemesa --- lib/dispatcher/client-h2.js | 8 +++-- test/http2-abort-rst-cancel.js | 58 ++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 test/http2-abort-rst-cancel.js diff --git a/lib/dispatcher/client-h2.js b/lib/dispatcher/client-h2.js index 9de79a4bc71..0d99db8fe05 100644 --- a/lib/dispatcher/client-h2.js +++ b/lib/dispatcher/client-h2.js @@ -90,6 +90,7 @@ const { HTTP2_HEADER_EXPECT, HTTP2_HEADER_STATUS, HTTP2_HEADER_PROTOCOL, + NGHTTP2_CANCEL, NGHTTP2_NO_ERROR, NGHTTP2_PROTOCOL_ERROR, NGHTTP2_REFUSED_STREAM @@ -1165,9 +1166,12 @@ function writeH2 (client, request) { if (state.stream != null) { clearRequestStream(request) - // On Abort, we close the stream to send RST_STREAM frame. + // On Abort, we close the stream to send RST_STREAM frame. Use CANCEL, + // the code for a stream that is no longer needed: with NO_ERROR, Node + // defers the RST_STREAM until a still-queued request body finishes, + // and the synchronous destroy below means that never happens. const stream = state.stream - stream.close() + stream.close(NGHTTP2_CANCEL) // close() alone leaves cleanup waiting on the 'close' event; on a busy, // long-lived multiplexed session that event can fail to fire, leaving the diff --git a/test/http2-abort-rst-cancel.js b/test/http2-abort-rst-cancel.js new file mode 100644 index 00000000000..1d5c3fb7dd2 --- /dev/null +++ b/test/http2-abort-rst-cancel.js @@ -0,0 +1,58 @@ +'use strict' + +const assert = require('node:assert') +const { test, after } = require('node:test') +const { constants, createSecureServer } = require('node:http2') +const { once } = require('node:events') + +const pem = require('@metcoder95/https-pem') + +const { Client } = require('..') + +// Aborting a request resets its stream with RST_STREAM(CANCEL), the code for a +// stream that is no longer needed. With the default NO_ERROR, Node defers the +// RST_STREAM until a still-queued request body has been written, and the +// synchronous destroy() in abort() means that never happens: the peer is never +// told the stream is gone. + +test('aborting a request with a queued body resets the stream with CANCEL', async () => { + // A zero initial window keeps the request body queued on the client. + const server = createSecureServer({ + ...(await pem.generate({ opts: { keySize: 2048 } })), + settings: { initialWindowSize: 0 } + }) + + const controller = new AbortController() + const reset = new Promise((resolve) => { + server.on('stream', (stream) => { + stream.on('error', () => {}) + stream.on('close', () => resolve(stream.rstCode)) + controller.abort() + }) + }) + + after(() => server.close()) + await once(server.listen(0), 'listening') + + const client = new Client(`https://localhost:${server.address().port}`, { + connect: { rejectUnauthorized: false }, + allowH2: true + }) + after(() => client.destroy()) + + const request = client.request({ + path: '/', + method: 'POST', + body: Buffer.alloc(32 * 1024), + signal: controller.signal + }).then(() => 'resolved', (err) => err.name) + + let timer + const rstCode = await Promise.race([ + reset, + new Promise((resolve) => { timer = setTimeout(() => resolve('no RST_STREAM'), 5000) }) + ]).finally(() => clearTimeout(timer)) + + assert.strictEqual(rstCode, constants.NGHTTP2_CANCEL) + assert.strictEqual(await request, 'AbortError') +}) From 3808fd1184f785141307af85fada15679e44987c Mon Sep 17 00:00:00 2001 From: gdemesa Date: Thu, 12 Nov 2026 10:26:37 -0800 Subject: [PATCH 2/2] docs: note that aborted HTTP/2 requests reset with CANCEL Co-Authored-By: Claude Opus 5.5 Signed-off-by: gdemesa --- docs/docs/api/Client.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/docs/api/Client.md b/docs/docs/api/Client.md index b81aef5559b..d6797eed813 100644 --- a/docs/docs/api/Client.md +++ b/docs/docs/api/Client.md @@ -169,6 +169,9 @@ eagerly. > * The server must support HTTP/2 and select it during ALPN negotiation, and > must not give HTTP/1.1 a higher priority than HTTP/2. > * `PUSH` frames are not supported. +> * Aborting a request, for example through its `signal`, resets its stream +> with `RST_STREAM` and the `CANCEL` error code, including when part of the +> request body has not been sent yet. ```mjs displayName="Basic instantiation" import { Client } from 'undici'