Repository navigation
fix: dispatch must not report busy after a synchronous failure - #6180
Merged
Merged
Conversation
DispatcherBase.dispatch catches synchronous request validation errors (e.g. an invalid header throwing UND_ERR_INVALID_ARG from the Request constructor) and, after delivering them through onResponseError, returned false. By convention a false return means the dispatcher has queued the request internally and is saturated: a 'drain' event will be emitted once it has capacity again. But a synchronously rejected request was never queued, consumed no capacity, and can never trigger a drain. PoolBase trusted the false return and permanently marked the idle client as needing drain (kNeedDrain), so with limited connections the client slot was never returned to rotation and every subsequent request waited in the pool queue forever with nothing running and no event left to resume it (nodejs#6054). Return true instead: the dispatcher did not take ownership of the request and remains available. This fixes all three PoolBase call sites (kDispatch, kOnDrain, kDrainQueue) and Agent at once, at the layer that owns the dispatch contract.
Regression tests for nodejs#6054, adapted from the reporter's reproduction and following the existing test/pool.js conventions: - a synchronously rejected request (invalid header) must not take the idle client out of rotation: the pool keeps serving requests with connections: 1 and the client slot is not consumed - a queued request that fails validation when the pool drains its queue must not end the drain loop or mark the just-drained client busy, leaving later queued requests stuck Replaces test/issue-6054-repro.js, whose scenario is now covered here.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #6180 +/- ##
==========================================
- Coverage 91.68% 91.68% -0.01%
==========================================
Files 113 113
Lines 44449 44454 +5
==========================================
+ Hits 40752 40756 +4
- Misses 3697 3698 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Merged
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.
Fixes #6054
Root cause
When a request fails synchronously during dispatch (e.g. an invalid header value like
\nraisesInvalidArgumentErrorinlib/core/request.jsbefore anything is queued),DispatcherBase.dispatch()(lib/dispatcher/dispatcher-base.js) catches the error, delivers it viahandler.onResponseError(), and then returnsfalse.But per undici's contract,
dispatch()returningfalsemeans "the request was accepted and the dispatcher is saturated — adrainevent will follow". Neither holds for a synchronous failure: nothing was queued, no capacity was consumed, and nodrainwill ever arrive.PoolBasetrusts that contract (lib/dispatcher/pool-base.js): it marks the idle clientkNeedDrain = true. Withrunning: 0there is no in-flight request whose completion could trigger a drain,kGetDispatcherskips need-drain clients, andconnections: 1prevents creating a new one — every subsequent valid request queues forever. With a finiteconnections: N, N validation failures exhaust all slots.Fix
lib/dispatcher/dispatcher-base.js— the catch path now returnstrue:This is fixed at the owner of the return-value contract rather than patching each consumer, so
PoolBase(kDispatch, thekOnDraindrain loop,kDrainQueue) andAgent's pass-through all benefit at once. Real saturation is still reported honestly by the normal path inClient[kDispatch], so capacity semantics are unchanged.Open question for maintainers (happy to adjust): for a closed/destroyed dispatcher whose dispatch fails synchronously, this returns
trueunconditionally; an alternative isreturn !this[kClosed] && !this[kDestroyed](as in #6055). I believe the unconditional form is safer forPoolBasebecause afalsethere would again mark a client as waiting for a drain that never comes — pool lifecycle already removes closed clients — but I'd like a maintainer's take on that edge.Tests
test/pool.js:kOnDraindrain-loop path.node --test test/{pool,client,dispatcher}.js: 109 tests — 108 pass / 0 fail / 1 env-skip.Note on prior attempts
#6055 (same-file approach, stalled since Oct 20) and #6081 (pool-side mitigation, now conflicting with main) both appear inactive; this PR revives the fix from the dispatcher-contract layer with the edge-case question called out above.
Disclosure
我在测试自研 Agentloop 的工作情况,选中此 Issue 为测试目标,已过验证。
(For clarity: this fix was produced by my own agent-loop implementation and has been verified as described above.)