http: option to mark active connections to close when idle - #66000
Open
WhatCats wants to merge 1 commit into
Open
http: option to mark active connections to close when idle#66000WhatCats wants to merge 1 commit into
WhatCats wants to merge 1 commit into
Conversation
`server.close()` now calls `closeIdleConnections(true)`, adding a `closeWhenIdle` parameter that marks a connection as due for closing once its current request/response finishes, even if it is active (not idle) at the moment `close()` is called. Previously such connections were left open for keep-alive reuse until `keepAliveTimeout` reaped them. A marked connection also stops advertising `Connection: keep-alive` on the response it is currently sending. Signed-off-by: WhatCats <whatcater@gmail.com> Assisted-by: Claude Code
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. |
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.
Summary
server.close()callscloseIdleConnections()internally (since bd7a808), which closes every connection that is idle at that exact moment. A connection that's actively sending a request or waiting for a response whenclose()runs is correctly left alone and once that response finishes, nothing ever rechecks it. It reverts to being a normal keep-alive connection and stays open untilkeepAliveTimeoutreaps it (65s by default), rather than closing as part of theserver.close()call.Interestingly, when investigating, I noticed this exact gap is also independently documented in Bun's source: https://github.com/oven-sh/bun/blob/df573dace6178d4ff94a797313bc8b5fdcf515e4/src/runtime/server/mod.rs#L1747-L1751
Reproduce
Call
server.close()while a request is in flight, using a client with an explicit keep-aliveAgent(so the client doesn't voluntarily tear down its side). The'close'event doesn't fire untilkeepAliveTimeout(+keepAliveTimeoutBuffer) elapses, even though the response completed almost immediately. (seetest/parallel/test-http-server-close-when-idle.js)Fix
server.close()now callscloseIdleConnections(true), adding acloseWhenIdleparameter that marks a connection as due for closing once its current request/response finishes, even if it is active (not idle) at the momentclose()is called. Previously such connections were left open for keep-alive reuse untilkeepAliveTimeoutreaped them. A marked connection also stops advertisingConnection: keep-aliveon the response it is currently sending.