Commit 0c5be9dfd6c for nodejs

commit 0c5be9dfd6cd508222ff33b27a7b5ab892a70379
Author: Matteo Collina <hello@matteocollina.com>
Date:   Fri Dec 4 09:22:53 2026 +0100

    http2: destroy compat stream on request timeout

    When a request times out and no 'timeout' handler is attached to the
    request or response, the HTTP/2 compatibility layer now destroys the
    stream, matching the HTTP/1 behavior where the underlying socket is
    destroyed. This prevents a response from being sent after the timeout.

    The compat layer re-emits the stream 'timeout' event on the request and
    response objects and only keeps the stream open if one of them handled
    it. Request/response setTimeout now register their callback on the
    request/response object, as HTTP/1 does, so that a Timeout callback
    counts as handling the event.

    Refs: https://github.com/nodejs/node/issues/67820
    Signed-off-by: Matteo Collina <hello@matteocollina.com>
    PR-URL: https://github.com/nodejs/node/pull/67832
    Fixes: https://github.com/nodejs/node/issues/67820
    Reviewed-By: Robert Nagy <ronagy@icloud.com>
    Reviewed-By: James M Snell <jasnell@gmail.com>
    Reviewed-By: Paolo Insogna <paolo@cowtech.it>
    Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>

diff --git a/lib/internal/http2/compat.js b/lib/internal/http2/compat.js
index c4a91c1e92a..9680170e45a 100644
--- a/lib/internal/http2/compat.js
+++ b/lib/internal/http2/compat.js
@@ -346,12 +346,17 @@ function onStreamCloseRequest() {
 // options; the Readable constructor only reads from it.
 const kDefaultRequestOptions = { autoDestroy: false };

-function onStreamTimeoutRequest() {
-  this[kRequest].emit('timeout');
-}
-
-function onStreamTimeoutResponse() {
-  this[kResponse].emit('timeout');
+function onStreamTimeout() {
+  const req = this[kRequest];
+  const res = this[kResponse];
+  // Matches the HTTP/1 semantics in `socketOnTimeout`: re-emit the 'timeout'
+  // event on the request and response objects, and only keep the stream open
+  // if one of them had a listener to handle it. Otherwise the stream is
+  // destroyed so that no response can be sent after the timeout.
+  const reqTimeout = req && req.emit('timeout');
+  const resTimeout = res && res.emit('timeout');
+  if (!reqTimeout && !resTimeout)
+    this.destroy();
 }

 class Http2ServerRequest extends Readable {
@@ -382,7 +387,6 @@ class Http2ServerRequest extends Readable {
     stream.on('error', onStreamError);
     stream.on('aborted', onStreamAbortedRequest);
     stream.on('close', onStreamCloseRequest);
-    stream.on('timeout', onStreamTimeoutRequest);
     this.on('pause', onRequestPause);
     this.on('resume', onRequestResume);
   }
@@ -511,8 +515,13 @@ class Http2ServerRequest extends Readable {
   }

   setTimeout(msecs, callback) {
-    if (!this[kState].closed)
-      this[kStream].setTimeout(msecs, callback);
+    if (!this[kState].closed) {
+      if (callback !== undefined) {
+        validateFunction(callback, 'callback');
+        this.on('timeout', callback);
+      }
+      this[kStream].setTimeout(msecs);
+    }
     return this;
   }
 }
@@ -578,7 +587,7 @@ class Http2ServerResponse extends Stream {
     stream.on('aborted', onStreamAbortedResponse);
     stream.on('close', onStreamCloseResponse);
     stream.on('wantTrailers', onStreamTrailersReady);
-    stream.on('timeout', onStreamTimeoutResponse);
+    stream.on('timeout', onStreamTimeout);
   }

   // User land modules such as finalhandler just check truthiness of this
@@ -1039,7 +1048,11 @@ class Http2ServerResponse extends Stream {
   setTimeout(msecs, callback) {
     if (this[kState].closed)
       return;
-    this[kStream].setTimeout(msecs, callback);
+    if (callback !== undefined) {
+      validateFunction(callback, 'callback');
+      this.on('timeout', callback);
+    }
+    this[kStream].setTimeout(msecs);
   }

   createPushResponse(headers, callback) {
diff --git a/test/parallel/test-http2-compat-timeout-destroy.js b/test/parallel/test-http2-compat-timeout-destroy.js
new file mode 100644
index 00000000000..f1d1e4af8c5
--- /dev/null
+++ b/test/parallel/test-http2-compat-timeout-destroy.js
@@ -0,0 +1,41 @@
+'use strict';
+
+const common = require('../common');
+if (!common.hasCrypto)
+  common.skip('missing crypto');
+const assert = require('assert');
+const http2 = require('http2');
+
+const timeout = common.platformTimeout(20);
+
+// When a request times out and no 'timeout' handler is attached to either the
+// request or the response, the HTTP/2 compat layer must destroy the stream,
+// matching the HTTP/1 behavior where the underlying socket is destroyed. This
+// prevents a response from being sent after the timeout.
+{
+  const server = http2.createServer();
+  let client;
+
+  server.on('request', common.mustCall((req, res) => {
+    // No 'timeout' listener is added anywhere.
+    req.setTimeout(timeout);
+    req.resume();
+
+    // By the time this fires, the stream timeout has already elapsed, so the
+    // stream must have been destroyed by the compat layer.
+    setTimeout(common.mustCall(() => {
+      assert.strictEqual(req.stream.destroyed, true);
+      server.close();
+      client.destroy();
+    }), timeout * 3);
+  }));
+
+  server.listen(0, common.mustCall(() => {
+    client = http2.connect(`http://localhost:${server.address().port}`);
+    client.on('error', () => {});
+    const req = client.request({ ':path': '/' });
+    req.on('error', () => {});
+    req.on('data', () => {});
+    req.end();
+  }));
+}