[v7.x] fix(agent): keep pools that still have work when a connection closes - #5916
Open
SulimanAbdulrazzaq wants to merge 1 commit into
Open
SulimanAbdulrazzaq wants to merge 1 commit into
SulimanAbdulrazzaq wants to merge 1 commit into
Conversation
Agent counted connect and disconnect events per origin key and closed the pool when the count reached zero. When a server ends a keep-alive socket with Connection: close, the disconnect can drop the count to zero while the next request is already queued on that pool, so the pool is closed after serving one more request. Its late disconnect then decrements the count of the pool that replaced it under the same key, which closes that pool too, and every later connection serves a single request. Decide from the pool's own state instead, as main does: ignore events from a pool that was already replaced, and keep a pool while it has connected clients, is busy, or has pending requests.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## v7.x #5916 +/- ##
=======================================
Coverage 93.11% 93.12%
=======================================
Files 112 112
Lines 37366 37371 +5
=======================================
+ Hits 34793 34800 +7
+ Misses 2573 2571 -2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This branch has not been deployed
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.
This relates to...
Fixes #5910 on v7.x (it is also the v7.x form of #5022). Node.js 24 bundles undici 7.x, so its global
fetchis affected.Rationale
On v7.x,
Agentcounts connect and disconnect events for each origin key and closes the pool when the count reaches 0. When a server ends a keep-alive socket withConnection: close:With the reproducer from #5910 (server closes each socket after 100 requests, 400 sequential fetches), the sockets served
[100,100,1,1,1,1,1,1]on v7.x.On
mainthis logic was already replaced (#5034, thenkPendingin #5740). This PR ports the same decision rule to v7.x.Changes
Bug Fixes
lib/dispatcher/agent.js:closeClientIfUnusednow decides from the pool's own state instead of the shared counter:kConnected > 0,kBusyorkPending > 0.{ dispatcher }entry shape is kept becauseMockAgentshares this map on v7.x. Only thecountfield is dropped.test/agent-connection-management.js(new): the server closes each socket after 3 requests, and the tests send 12 sequential requests through anAgent, one test withrequest()and one withfetch(). Both expect[3, 3, 3, 3]requests per socket.Not included: the
maxRequestsPerClientcounter change from #5034 inclient-h1.js, because it is a separate fix.Breaking Changes and Deprecations
N/A
Status
Tested on Node 24:
[3, 1, 1, 1, 1, 1, 1, 1, 1, 1](request()) and[3, 3, 1, 1, 1, 1, 1, 1](fetch()).test:unit,test:node-test,test:fetch,test:node-fetch,test:interceptors,test:typescriptand ESLint on the changed files.[100,100,100,100,1]with the fix.