Skip to content

Commit 57cddb6

Browse files
committed
http: fixes keep-alive socket reuse race in requestOnFinish
When the HTTP response ends before the request's 'finish' event fires, responseOnEnd() and requestOnFinish() can both call responseKeepAlive(), corrupting the socket that the agent may have already handed to another request. This commit adds a !req.destroyed guard to requestOnFinish() so the second call is skipped when the socket has already been released. Fixes: #60001
1 parent 5f312b3 commit 57cddb6

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

lib/_http_client.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,11 @@ function responseOnTimeout() {
870870
function requestOnFinish() {
871871
const req = this;
872872

873-
if (req.shouldKeepAlive && req._ended)
873+
// If the response ends before this request finishes writing, `responseOnEnd()`
874+
// already released the socket. When `finish` fires later, that socket may
875+
// belong to a different request, so only call `responseKeepAlive()` when the
876+
// original request is still alive (`!req.destroyed`).
877+
if (req.shouldKeepAlive && req._ended && !req.destroyed)
874878
responseKeepAlive(req);
875879
}
876880

0 commit comments

Comments
 (0)