From 0efefc15a769790536c3fd3500b028139dd416ca Mon Sep 17 00:00:00 2001 From: Amol Yadav Date: Sat, 7 Feb 2026 10:53:31 +0530 Subject: [PATCH] http2: fix keepAliveTimeout with allowHTTP1 --- lib/internal/http2/core.js | 15 +++++++++++++++ ...st-http2-https-fallback-http-server-options.js | 3 +++ 2 files changed, 18 insertions(+) diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 8b526c001004c5..3787a0a85116ba 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -5,6 +5,7 @@ const { ArrayIsArray, MathMin, Number, + NumberIsFinite, ObjectAssign, ObjectDefineProperty, ObjectEntries, @@ -3389,6 +3390,20 @@ class Http2SecureServer extends TLSServer { this.headersTimeout = 60_000; // Minimum between 60 seconds or requestTimeout this.requestTimeout = 300_000; // 5 minutes this.connectionsCheckingInterval = 30_000; // 30 seconds + const keepAliveTimeout = options.keepAliveTimeout; + if (keepAliveTimeout !== undefined) { + validateInteger(keepAliveTimeout, 'keepAliveTimeout', 0); + this.keepAliveTimeout = keepAliveTimeout; + } else { + this.keepAliveTimeout = 5_000; // 5 seconds; + } + const keepAliveTimeoutBuffer = options.keepAliveTimeoutBuffer; + // Optional buffer added to the keep-alive timeout when setting socket timeouts. + // Helps reduce ECONNRESET errors from clients by extending the internal timeout. + // Default is 1000ms if not specified. + const buf = keepAliveTimeoutBuffer; + this.keepAliveTimeoutBuffer = + (typeof buf === 'number' && NumberIsFinite(buf) && buf >= 0) ? buf : 1000; this.shouldUpgradeCallback = function() { return this.listenerCount('upgrade') > 0; }; diff --git a/test/parallel/test-http2-https-fallback-http-server-options.js b/test/parallel/test-http2-https-fallback-http-server-options.js index 8143f56d491ccc..9192c441ff60ec 100644 --- a/test/parallel/test-http2-https-fallback-http-server-options.js +++ b/test/parallel/test-http2-https-fallback-http-server-options.js @@ -20,6 +20,9 @@ const ca = fixtures.readKey('fake-startcom-root-cert.pem'); function onRequest(request, response) { const { socket: { alpnProtocol } } = request.httpVersion === '2.0' ? request.stream.session : request; + // Verify that keepAliveTimeout is set when allowHTTP1 is true + assert.strictEqual(typeof request.socket.server.keepAliveTimeout, 'number'); + assert.strictEqual(request.socket.server.keepAliveTimeout, 5000); response.status(200); response.end(JSON.stringify({ alpnProtocol,