Skip to content

http: don't destroy connections still writing a response - #67915

Open
glasser wants to merge 1 commit into
nodejs:mainfrom
glasser:http-close-idle-writable-finished
Open

glasser wants to merge 1 commit into
nodejs:mainfrom
glasser:http-close-idle-writable-finished

Conversation

@glasser

@glasser glasser commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

closeIdleConnections() skips a connection only while its response is not finished, but finished is set as soon as end() is called, before the queued data has been written to the socket. A response to a slow client can therefore be destroyed mid-write by closeIdleConnections(), or by server.close() which calls it, truncating the body while the end() callback and 'finish' still fire on the server.

This checks writableFinished instead, which is only true once everything has been handed to the socket (finished && outputSize === 0 && socket.writableLength === 0). A connection that passes the new check has written everything, so destroying it loses nothing. closeAllConnections() is unchanged.

The new test ends a 32 MiB response and calls server.close() while the body is still queued; it fails on current main with Error: aborted on the client and passes with the change. The existing test-http-server-close-idle* tests still pass.

(Note that a connection skipped by the sweep reverts to a normal keep-alive connection after its response; nothing automatically cleans up non-idle connections later. #66000 fixes that issue and this PR is compatible with that one (they merge cleanly with tests passing).)

Fixes: #67910
Refs: #43890

AI disclosure

As described in the issue, the bug was found and the initial draft of the fix and test were written with help from a coding agent. I reviewed and verified the change (including rewriting comments).

@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 Nov 30, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Caution

AgentScan found account activity patterns that may be consistent with automation. This is a heuristic, not proof that this pull request was opened by an agent or violates policy. AI-assisted contributions are permitted, but automated tooling must not open pull requests without advance approval, and contributors must personally understand, test, verify, and take responsibility for every submitted change. See the AgentScan analysis, AI use policy, and automation policy for additional context.

@glasser

glasser commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor Author

AgentScan is correct that (as acknowledged) this change was originally written by an AI agent, but I understand and stand by the submitted version of the change. (Its main signal for concluding agent activity is that I pushed a stack of AI-generated PRs to an open source repo I maintained quickly before going on Thanksgiving vacation; turning those PRs into real PRs worthy of release is my main job this week :) )

Note that the test added in the PR works a bit differently from the test in the issue. The issue is a bit more "realistic" — it just shows an HTTP client that pauses and has delays, as can happen via network delays. The PR's test is deterministic without timeouts by calling server.close() directly within the request handler (which doesn't reflect realistic usage).

closeIdleConnections() skips a connection only while its response is
not `finished`, but `finished` is set as soon as end() is called, before
the queued data has been written to the socket. A response to a slow
client can therefore be destroyed mid-write by closeIdleConnections(),
or by server.close() which calls it, truncating the body while the end()
callback and 'finish' still fire. Check `writableFinished` instead,
which is only true once everything has been handed to the socket.

Fixes: nodejs#67910
Refs: nodejs#43890
Signed-off-by: David Glasser <glasser@apollographql.com>
@codecov

codecov Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.57%. Comparing base (f3af49d) to head (bd4d561).
⚠️ Report is 13 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67915      +/-   ##
==========================================
- Coverage   90.58%   90.57%   -0.01%     
==========================================
  Files         802      802              
  Lines      286655   286658       +3     
  Branches    55373    55371       -2     
==========================================
- Hits       259666   259641      -25     
- Misses      17092    17128      +36     
+ Partials     9897     9889       -8     
Files with missing lines Coverage Δ
lib/internal/http/server.js 96.46% <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

agentscan:mixed-signals 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: server.close() destroys connections that are still writing a response, truncating them

2 participants