Commit 408fdc5efbe for nodejs
commit 408fdc5efbef29da2fece096638e3f12af43f66b
Author: Yuya Inoue <65857152+inoway46@users.noreply.github.com>
Date: Wed Dec 2 00:51:38 2026 +0900
http2: destroy stream on compat request destroy
Http2ServerRequest never implemented destroy(), although the docs say
it destroys the Http2Stream. Only the request's readable side was
destroyed, so the stream stayed open and a response could still be
sent. The request could also emit 'close' twice.
Destroy the stream when the request is destroyed before it was read to
the end, like http1's IncomingMessage does with its socket. After
'end' the stream is left alone, so `for await (const chunk of req)`
does not abort the response.
Signed-off-by: inoway46 <inoueyuya416@gmail.com>
Assisted-by: Claude Code
PR-URL: https://github.com/nodejs/node/pull/67821
Refs: https://github.com/nodejs/node/pull/67774
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
diff --git a/doc/api/http2.md b/doc/api/http2.md
index ade269150da..5d45b0fef70 100644
--- a/doc/api/http2.md
+++ b/doc/api/http2.md
@@ -4357,11 +4357,12 @@ added: v8.4.0
* `error` {Error}
-Calls `destroy()` on the [`Http2Stream`][] that received
+Destroys the request. If the request has not been read to the end, this also
+calls `destroy()` on the [`Http2Stream`][] that received
the [`Http2ServerRequest`][]. If `error` is provided, an `'error'` event
is emitted and `error` is passed as an argument to any listeners on the event.
-It does nothing if the stream was already destroyed.
+It does nothing if the request was already destroyed.
#### `request.headers`
diff --git a/lib/internal/http2/compat.js b/lib/internal/http2/compat.js
index 73a1ec98462..c4a91c1e92a 100644
--- a/lib/internal/http2/compat.js
+++ b/lib/internal/http2/compat.js
@@ -326,8 +326,9 @@ function onStreamCloseRequest() {
// The peer closed the stream before END_STREAM, so the request body is
// incomplete. Like http1's IncomingMessage on abort, destroy the request
// instead of ending it, so 'end' is not emitted for a truncated body.
- // destroy() emits 'close'.
- if (this[kIncomplete]) {
+ // destroy() emits 'close'. If the request was already destroyed, it has
+ // emitted 'close' or is about to, so do not emit it a second time.
+ if (this[kIncomplete] || req.destroyed) {
req.destroy();
return;
}
@@ -464,6 +465,23 @@ class Http2ServerRequest extends Readable {
}
}
+ _destroy(err, cb) {
+ const stream = this[kStream];
+ // Like http1's IncomingMessage, which destroys the socket, destroy the
+ // underlying stream if the request is destroyed before it was read to
+ // the end. Leave the stream alone after 'end', so that for example
+ // `for await (const chunk of req)` does not abort the response.
+ if (!this.readableEnded && !stream.destroyed) {
+ // onStreamError forwards the error to the request.
+ stream.destroy(err);
+ cb();
+ return;
+ }
+ // Mirror HTTP/1's IncomingMessage._destroy: emit 'error' only when a
+ // listener is attached.
+ cb(err != null && this.listenerCount('error') > 0 ? err : null);
+ }
+
get method() {
return this[kHeaders][HTTP2_HEADER_METHOD];
}
diff --git a/test/parallel/test-http2-compat-serverrequest-destroy.js b/test/parallel/test-http2-compat-serverrequest-destroy.js
new file mode 100644
index 00000000000..2a50b7d62b6
--- /dev/null
+++ b/test/parallel/test-http2-compat-serverrequest-destroy.js
@@ -0,0 +1,163 @@
+'use strict';
+
+const common = require('../common');
+if (!common.hasCrypto)
+ common.skip('missing crypto');
+
+// Like http1's IncomingMessage, destroying a compat request before it was
+// read to the end destroys the underlying Http2Stream. Destroying it after
+// 'end' leaves the stream alone, so the response can still be sent.
+
+const assert = require('assert');
+const http2 = require('http2');
+
+const {
+ NGHTTP2_NO_ERROR,
+ NGHTTP2_INTERNAL_ERROR,
+ NGHTTP2_CANCEL,
+} = http2.constants;
+
+// Starts a server with the given handler and sends one POST request with a
+// body that is never ended. Expects the stream to be reset with `code`.
+function testReset(code, handler) {
+ const server = http2.createServer(handler);
+ server.listen(0, common.mustCall(() => {
+ const client = http2.connect(`http://localhost:${server.address().port}`);
+ const req = client.request({ ':method': 'POST' });
+ req.write('partial');
+ req.on('response', common.mustNotCall());
+ req.on('error', () => {});
+ req.on('close', common.mustCall(() => {
+ assert.strictEqual(req.rstCode, code);
+ client.close();
+ server.close();
+ }));
+ }));
+}
+
+// Starts a server with the given handler and sends one complete POST request.
+// Expects a complete 200 response with the body 'ok'.
+function testResponse(handler) {
+ const server = http2.createServer(handler);
+ server.listen(0, common.mustCall(() => {
+ const client = http2.connect(`http://localhost:${server.address().port}`);
+ const req = client.request({ ':method': 'POST' });
+ req.end('body');
+ req.on('response', common.mustCall((headers) => {
+ assert.strictEqual(headers[':status'], 200);
+ }));
+ let data = '';
+ req.setEncoding('utf8');
+ req.on('data', (chunk) => { data += chunk; });
+ req.on('end', common.mustCall(() => {
+ assert.strictEqual(data, 'ok');
+ }));
+ req.on('close', common.mustCall(() => {
+ assert.strictEqual(req.rstCode, NGHTTP2_NO_ERROR);
+ client.close();
+ server.close();
+ }));
+ }));
+}
+
+// destroy() before the request ended destroys the stream.
+testReset(NGHTTP2_NO_ERROR, common.mustCall((req, res) => {
+ req.on('end', common.mustNotCall());
+ req.on('error', common.mustNotCall());
+ req.on('aborted', common.mustCall());
+ req.on('close', common.mustCall());
+ res.on('close', common.mustCall());
+
+ assert.strictEqual(req.destroy(), req);
+ assert.strictEqual(req.destroyed, true);
+ assert.strictEqual(req.stream.destroyed, true);
+
+ // Destroying it again does nothing.
+ req.destroy(new Error('ignored'));
+}));
+
+// destroy(err) before the request ended destroys the stream with the error
+// and emits it on the request.
+{
+ const err = new Error('kaboom');
+ testReset(NGHTTP2_INTERNAL_ERROR, common.mustCall((req, res) => {
+ req.on('end', common.mustNotCall());
+ req.on('error', common.mustCall((actual) => {
+ assert.strictEqual(actual, err);
+ }));
+ req.on('close', common.mustCall());
+ res.on('close', common.mustCall());
+
+ req.destroy(err);
+ assert.strictEqual(req.stream.destroyed, true);
+ }));
+}
+
+// destroy(err) without an 'error' listener on the request does not throw.
+testReset(NGHTTP2_INTERNAL_ERROR, common.mustCall((req, res) => {
+ req.on('close', common.mustCall());
+ req.destroy(new Error('kaboom'));
+ assert.strictEqual(req.stream.destroyed, true);
+}));
+
+// destroy() while the stream is closing does not emit 'close' twice.
+testReset(NGHTTP2_NO_ERROR, common.mustCall((req, res) => {
+ req.on('close', common.mustCall());
+ req.stream.destroy();
+ req.destroy();
+}));
+
+// destroy() after 'end' leaves the stream open.
+testResponse(common.mustCall((req, res) => {
+ req.resume();
+ req.on('aborted', common.mustNotCall());
+ req.on('close', common.mustCall());
+ req.on('end', common.mustCall(() => {
+ req.destroy();
+ assert.strictEqual(req.destroyed, true);
+ assert.strictEqual(req.stream.destroyed, false);
+ setImmediate(() => res.end('ok'));
+ }));
+}));
+
+// destroy(err) after 'end' emits the error on the request only.
+{
+ const err = new Error('kaboom');
+ testResponse(common.mustCall((req, res) => {
+ req.resume();
+ req.on('error', common.mustCall((actual) => {
+ assert.strictEqual(actual, err);
+ }));
+ req.on('close', common.mustCall());
+ req.on('end', common.mustCall(() => {
+ req.destroy(err);
+ assert.strictEqual(req.stream.destroyed, false);
+ setImmediate(() => res.end('ok'));
+ }));
+ }));
+}
+
+// Reading the whole request with for await...of, which destroys the request
+// when it is done, does not abort the response.
+testResponse(common.mustCall(async (req, res) => {
+ req.on('aborted', common.mustNotCall());
+ req.on('close', common.mustCall());
+ let body = '';
+ req.setEncoding('utf8');
+ for await (const chunk of req)
+ body += chunk;
+ assert.strictEqual(body, 'body');
+ assert.strictEqual(req.stream.destroyed, false);
+ res.end('ok');
+}));
+
+// Leaving for await...of early destroys the request with an AbortError,
+// which resets the stream with NGHTTP2_CANCEL.
+testReset(NGHTTP2_CANCEL, common.mustCall(async (req, res) => {
+ req.on('close', common.mustCall(() => {
+ assert.strictEqual(req.destroyed, true);
+ }));
+ // eslint-disable-next-line no-unused-vars
+ for await (const chunk of req)
+ break;
+}));