fix: run idle socket validation off setImmediate instead of setTimeout - #5499
Merged
mcollina merged 1 commit intoJul 11, 2026
Merged
Conversation
carlotestor
force-pushed
the
fix/idle-validation-setimmediate
branch
from
July 3, 2026 12:33
8dcfcfa to
137d592
Compare
7 tasks
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5499 +/- ##
=======================================
Coverage 93.43% 93.43%
=======================================
Files 110 110
Lines 37278 37278
=======================================
Hits 34830 34830
Misses 2448 2448 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Merged
6 tasks
mcollina
added a commit
that referenced
this pull request
Jul 29, 2026
…on idle event loop (#5606) * fix: revert idle socket validation to setTimeout(0) to prevent stall on idle event loop The change from setTimeout(0) to setImmediate() in #5499 introduced a regression where reusing an idle keep-alive connection could stall for ~500ms. The root cause is the interaction between setImmediate().unref() and socket.unref() in the idle state: when the socket is unref'd and the only remaining handle is an unref'd setImmediate, the event loop poll phase waits for its timeout before reaching the check phase. setTimeout(0) fires in the timers phase (before poll), avoiding this problem entirely at the cost of ~1ms timer resolution per reuse. Fixes #5600 * test: add keep-alive socket reuse stall regression test * test: assert keep-alive reuse progress via logical I/O events Replace wall-clock timing and artificial server delays with server connection/request event coordination. The test timeout catches the setImmediate poll-phase stall regression without flaky Date.now checks. Signed-off-by: Matteo Collina <hello@matteocollina.com> --------- Signed-off-by: Matteo Collina <hello@matteocollina.com> Co-authored-by: Matteo Collina <hello@matteocollina.com>
marko1olo
added a commit
to marko1olo/undici
that referenced
this pull request
Aug 14, 2026
Reusing an idle keep-alive socket can stall for up to a fast-timers tick (~500ms) before the request reaches the wire. With a pool of one connection and a 10ms server, six sequential requests take over a second instead of the ~60ms the server accounts for. `scheduleIdleSocketValidation` moved from an unref'd `setTimeout(..., 0)` to `setImmediate` in nodejs#5499, and the `unref?.()` came along with it. An unref'd immediate does not hold the loop open, so on an otherwise idle loop the check phase can be reached only once the next fast-timers tick wakes things up, and the queued request waits for it. The request the validation exists for is already queued at that point, so there is nothing to keep alive artificially: the immediate is a one-shot that runs on the very next check phase and `clearIdleSocketValidation` cancels it when the socket goes away. test/issue-5600.js fails on the current code with a slowest request around 425ms and passes with the immediate left ref'd; on this machine the six requests go from 1095/1565/2045ms across runs to 122/137/219ms. Signed-off-by: marko1olo <marko1olo@users.noreply.github.com>
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
Fixes #5493
Schedule the idle-socket validation deferral with
setImmediateinstead ofsetTimeout(..., 0)inscheduleIdleSocketValidation(lib/dispatcher/client-h1.js), and cancel it withclearImmediateaccordingly (clearTimeoutdoes not cancel anImmediate, so the pending callback would otherwise fire after socket cleanup — harmlessly, since it self-guards, but the handle should be cancelled properly). The now-meaningless0delay argument is dropped.The validation only needs the event loop to surface any unsolicited bytes / FIN / RST already pending on the reused idle socket before the next request is written. The
setImmediatecheck phase still runs after the poll phase, so those events are processed first — but without the ~1ms+ timer floor thatsetTimeout(0)pays on every idle-socket reuse.Measured
Loopback HTTP, keep-alive Agent (
pipelining: 1), 2000 sequential requests + 8-wide bursts, node v22.22.1 (full harness in #5493):setTimeout(0))setImmediate)i.e. the GHSA-35p6-xmwp-9g52 mitigation is retained at effectively zero latency cost.
Tests
node --test test/response-queue-poisoning.js test/issue-810.js test/client-keep-alive.js— 15/15 pass with the patch (including the poisoned-idle-socket regression test)Notes
kIdleSocketValidationTimeoutsymbol now holds anImmediate; left the name unchanged to keep the diff minimal, happy to rename if preferred.Immediate.unref()exists, so the existing.unref?.()call keeps working.Disclosure
This pull request (analysis, patch, benchmarks, and text) was created by Claude (model: claude-fable), an AI assistant by Anthropic, operating under human direction. Please review accordingly.