Skip to content

http: don't destroy socket after request completes - #65952

Open
barathraj048 wants to merge 1 commit into
nodejs:mainfrom
barathraj048:http-no-abort-after-response-complete
Open

http: don't destroy socket after request completes#65952
barathraj048 wants to merge 1 commit into
nodejs:mainfrom
barathraj048:http-no-abort-after-response-complete

Conversation

@barathraj048

@barathraj048 barathraj048 commented Sep 10, 2026

Copy link
Copy Markdown

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 on a socket with no listener and crashes the process.

Repro (from the linked issue): abort a request via AbortSignal from inside for await iteration of the response body, using an agent with keepAlive: true. Even though both the request and response have 'error' handlers registered, the process exits with an unhandled 'error' event on the underlying Socket.

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: false requests, which already silently drop any unread buffered response data once the exchange is complete — so this doesn't introduce new behavior, it brings keepAlive: true in line with the non-keep-alive path.

Alternative considered

A broader fix would guard responseKeepAlive() (and its caller requestOnFinish()) against recycling a socket that already has a pending destroy/error, so the abort error would still reach request.on('error') instead of being swallowed. That's arguably the more complete fix for the underlying listener-lifecycle gap — see the existing TODO(ronag) comment in _http_client.js and the related note in _http_agent.js's on('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 a
follow-up if maintainers prefer it.

Testing

  • Added 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.
  • Ran the surrounding suites via 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

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>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/http
  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added http Issues and PRs related to the http subsystem. needs-ci PRs that need a full CI run. labels Sep 10, 2026
@github-actions

Copy link
Copy Markdown
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

codecov Bot commented Sep 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.17%. Comparing base (b3fb344) to head (170744a).
⚠️ Report is 9 commits behind head on main.

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     
Files with missing lines Coverage Δ
lib/_http_client.js 97.65% <100.00%> (+0.01%) ⬆️

... and 33 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

http Issues and PRs related to the http subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

http: aborting a keep-alive response during async iteration emits an unhandled Socket error

2 participants