diff --git a/lib/_http_agent.js b/lib/_http_agent.js index a4829526f6e138..86ad95fc9bd1f4 100644 --- a/lib/_http_agent.js +++ b/lib/_http_agent.js @@ -496,7 +496,10 @@ Agent.prototype.keepSocketAlive = function keepSocketAlive(socket) { const hint = RegExpPrototypeExec(/^timeout=(\d+)/, keepAliveHint)?.[1]; if (hint) { - const serverHintTimeout = NumberParseInt(hint) * 1000; + // Let the timer expires before the announced timeout to reduce + // the likelihood of ECONNRESET errors + let serverHintTimeout = ( NumberParseInt(hint) * 1000 ) - 1000; + serverHintTimeout = serverHintTimeout > 0 ? serverHintTimeout : 0; if (serverHintTimeout < agentTimeout) { agentTimeout = serverHintTimeout; diff --git a/lib/_http_server.js b/lib/_http_server.js index d67669fea9433b..bbfda91401d26f 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -1010,7 +1010,9 @@ function resOnFinish(req, res, socket, state, server) { } } else if (state.outgoing.length === 0) { if (server.keepAliveTimeout && typeof socket.setTimeout === 'function') { - socket.setTimeout(server.keepAliveTimeout); + // Increase the internal timeout wrt the advertised value to reduce likeliwood of ECONNRESET errors + // due to race conditions between the client and server timeout calculation + socket.setTimeout(server.keepAliveTimeout + 1000); state.keepAliveTimeoutSet = true; } } else {