http: don't destroy socket after request completes - #65952
Open
barathraj048 wants to merge 1 commit into
Open
Conversation
Aborting a ClientRequest after the request has finished sending and the response has fully arrived has nothing left to cancel. destroy() still called socket.destroy(err), but the resulting 'error' is emitted on a later tick. In that window, responseKeepAlive() has already removed socketErrorListener while handing the socket back to the agent's free pool, so the error lands with no listener and crashes the process. Skip the socket destroy when there is nothing left to cancel. This matches the existing behavior of keepAlive: false requests, which already drop any unread buffered response data in this situation. Fixes: nodejs#65938 Signed-off-by: Barath <barathraj048@gmail.com>
Collaborator
|
Review requested:
|
Contributor
|
Welcome to Node.js, and thank you for your first contribution! Before review, please take a moment to read:
Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #65952 +/- ##
=======================================
Coverage 90.16% 90.17%
=======================================
Files 771 771
Lines 265434 265497 +63
Branches 50450 50477 +27
=======================================
+ Hits 239332 239401 +69
Misses 17045 17045
+ Partials 9057 9051 -6
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Aborting a
ClientRequestafter the request has finished sending and the response has fully arrived has nothing left to cancel.destroy()still calledsocket.destroy(err), but the resulting'error'is emitted on a later tick. In that window,responseKeepAlive()has already removedsocketErrorListenerwhile handing the socket back to the agent's freepool, so the error lands on a socket with no listener and crashes the process.
Repro (from the linked issue): abort a request via
AbortSignalfrom insidefor awaititeration of the response body, using an agent withkeepAlive: true. Even though both the request and response have'error'handlers registered, the process exits with an unhandled'error'event on the underlyingSocket.The fix
Skip the socket destroy in
ClientRequest.prototype.destroy()when the request has already finished sending (writableFinished) and the response has fully arrived (res.complete). There is nothing left for an abort to cancel at that point.This matches the existing behavior of
keepAlive: falserequests, which already silently drop any unread buffered response data once the exchange is complete — so this doesn't introduce new behavior, it bringskeepAlive: truein line with the non-keep-alive path.Alternative considered
A broader fix would guard
responseKeepAlive()(and its callerrequestOnFinish()) against recycling a socket that already has a pending destroy/error, so the abort error would still reachrequest.on('error')instead of being swallowed. That's arguably the more complete fix for the underlying listener-lifecycle gap — see the existingTODO(ronag)comment in_http_client.jsand the related note in_http_agent.js'son('free', ...)handler — but it touches connection-pooling behavior for all keep-alive traffic, so I went with the narrower, lower-risk fix here. Happy to attempt the broader fix as afollow-up if maintainers prefer it.
Testing
test/parallel/test-http-client-abort-completed-keepalive.js, which reproduces the crash on an unpatched tree and passes after this change. It also asserts the socket is returned to the agent's pool and reused by a subsequent request.tools/test.py:parallel/test-http-agent*,parallel/test-http-client-abort*,parallel/test-http-client-timeout*,parallel/test-http-keep-alive*— 58/58 passed.
Fixes: #65938
Affected core subsystem(s)
http