Skip to content

http: add timeout option to server.close() - #67851

Open
araujogui wants to merge 2 commits into
nodejs:mainfrom
araujogui:http-server-close-timeout
Open

araujogui wants to merge 2 commits into
nodejs:mainfrom
araujogui:http-server-close-timeout

Conversation

@araujogui

Copy link
Copy Markdown
Member

server.close() waits forever for active connections, so one slow or stuck request keeps the server from closing.

This adds a timeout option, in milliseconds, to close() on http.Server and https.Server. Connections still open when it elapses are destroyed with closeAllConnections().

HTTP/2 servers are not covered.

server.close() waits indefinitely for connections that are sending a
request or waiting for a response, so a single slow or stuck request
keeps the server from closing.

Add an optional `options` argument with a `timeout` in milliseconds to
`http.Server` and `https.Server`. Connections that are still open when
it elapses are destroyed with `closeAllConnections()`.

Assisted-by: Claude
Signed-off-by: Guilherme Araújo <arauujogui@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 28, 2026 16:23
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/crypto
  • @nodejs/http
  • @nodejs/net

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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Signed-off-by: Guilherme Araújo <arauujogui@gmail.com>
@codecov

codecov Bot commented Nov 28, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.48387% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.53%. Comparing base (d3f8545) to head (8b74e83).
⚠️ Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/http/server.js 84.74% 9 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67851      +/-   ##
==========================================
- Coverage   90.53%   90.53%   -0.01%     
==========================================
  Files         805      805              
  Lines      285842   285964     +122     
  Branches    55187    55223      +36     
==========================================
+ Hits       258793   258898     +105     
- Misses      17167    17178      +11     
- Partials     9882     9888       +6     
Files with missing lines Coverage Δ
lib/https.js 97.25% <100.00%> (-0.01%) ⬇️
lib/internal/http/server.js 96.27% <84.74%> (-0.39%) ⬇️

... and 30 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.

Comment on lines +633 to +636
if (this[kCloseTimeout] != null) {
clearTimeout(this[kCloseTimeout]);
this[kCloseTimeout] = null;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This clears kCloseTimeout but leaves clearCloseTimeout attached on 'close'. If close({ timeout }) gets called again before 'close' fires (like after calling listen() again, or if upgraded sockets keep the server open), it'll attach a duplicate listener

Same thing in onCloseTimeout

Suggested change
if (this[kCloseTimeout] != null) {
clearTimeout(this[kCloseTimeout]);
this[kCloseTimeout] = null;
}
if (this[kCloseTimeout] != null) {
clearTimeout(this[kCloseTimeout]);
this[kCloseTimeout] = null;
this.removeListener('close', clearCloseTimeout);
}

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. https Issues and PRs related to the https subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants