fix(h1): do not reuse idle sockets whose keep-alive timeout expired #6171
All Vouch review confidence checks passed
Residual risk 0.10 = Change Risk (0.33) × (1 − Review Confidence (0.71)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.33 · 114 lines diff across 2 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.56 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.284
Real PR Conversation Timeline
3 items
Codecov Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 91.68%. Comparing base (
76f0b3e) to head (8f1768a).:warning: Report is 1 commits behind head on main.
<details><summary>Additional details and impacted files</summary>
@@ Coverage Diff @@
## main #6171 +/- ##
=======================================
Coverage 91.67% 91.68%
=======================================
Files 113 113
Lines 44419 44467 +48
=======================================
+ Hits 40721 40768 +47
- Misses 3698 3699 +1
</details>
:umbrella: View full report in Codecov by Harness.
:loudspeaker: Have feedback on the report? Share it here.
<details><summary> :rocket: New features to boost your workflow: </summary>
</details>
Approved
Approved these changes.
ronag
mcollina
This is the undici counterpart of https://github.com/nodejs/node/pull/67292.
Problem
The keep-alive timer of an idle HTTP/1.1 socket cannot fire while the event loop is blocked. Before reusing an idle socket, undici yields one
setImmediate(scheduleIdleSocketValidation, added for GHSA-35p6-xmwp-9g52), so that a FIN or RST that has already been received is processed first. That only works if a poll phase runs between the dispatch and the check phase.With a server in a separate process (
keepAliveTimeout = 5000), one request, then a 6s synchronous block, then a second request:| Second request dispatched from | Processed before the validation completes | Result on
main||---|---|---|
| poll phase (I/O callback, server request handler,
awaitafter I/O) | nothing: the check phase comes next |ECONNRESET: the dead socket is reused || timers phase | the poll phase reads the FIN | reconnects |
| check phase (
setImmediate) | the keep-alive timer fires | reconnects |The poll-phase case is the common one, for example a proxy that does synchronous work in its request handler before calling upstream. A shorter version of the same problem: a block longer than the client's keep-alive timeout but shorter than the server's uses up the
keepAliveTimeoutThresholdmargin, and the request is sent just before the server closes the connection.Fix
resumeH1arms the keep-alive timer, recordperformance.now()on the socket.InformationalError('socket idle timeout')if its keep-alive timeout has passed. Nothing is running on the socket, so the pending requests are sent on a new connection, the same as when the timer fires.The time is recorded when the timer is armed, so the check never expires a socket before the timer would have. It uses the global
performance.now(), so it follows@sinonjs/fake-timerstogether withsetTimeout. The cost is oneperformance.now()call when a socket becomes idle and one when it is reused.The check does not depend on
kSocketUsed, so it also covers the experimental Milo parser.parser-h1.jsnever sets that symbol, which is private toclient-h1.js. As a result, the existing idle socket validation does not run with Milo. That is a separate issue and is left for a follow-up.Not covered
As in the Node.js change:
HTTP/2 is not changed.
Testing
test/client-keep-alive-blocked-loop.jsblocks inside anfscallback (poll phase) and checks that an expired socket is replaced and that a socket that has not expired is reused. The expiry case fails onmainand passes with this change, with both llhttp andUNDICI_USE_MILO=1.test/.js: 1664 passed, 0 failed, 3 skipped.test/node-test/*: 245 passed, 0 failed, 5 skipped.🤖 Generated with Claude Code
https://claude.ai/code/session_01Sfj4nEWQ4ovycNDjbvXcRs