Skip to content

Commit

Permalink
Reduce likeliwood of race conditions on keep-alive timeout calculatio…
Browse files Browse the repository at this point in the history
…n between http1.1 servers and clients
  • Loading branch information
zanettea authored and Zanette Arrigo committed Apr 23, 2024
1 parent 5548928 commit e617573
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Check failure on line 501 in lib/_http_agent.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

There should be no space after this paren

Check failure on line 501 in lib/_http_agent.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

There should be no space before this paren
serverHintTimeout = serverHintTimeout > 0 ? serverHintTimeout : 0;

if (serverHintTimeout < agentTimeout) {
agentTimeout = serverHintTimeout;
Expand Down
4 changes: 3 additions & 1 deletion lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e617573

Please sign in to comment.