fix: MockAgent delayed response with AbortSignal (#4693)#4772
Merged
Conversation
When a MockAgent had a delayed response and the request was aborted via AbortSignal, the delayed response callback would still fire and attempt to call handler methods after onError had been called. This violated state invariants expected by handlers like DecoratorHandler (used by decompress interceptor). The fix: - Tracks abort state in mockDispatch - Clears pending setTimeout timer when abort is called - Checks abort flag in handleReply before invoking handler callbacks - Properly calls handler.onConnect to register abort callback Fixes #4693
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4772 +/- ##
==========================================
- Coverage 93.27% 93.24% -0.03%
==========================================
Files 109 109
Lines 34001 34034 +33
==========================================
+ Hits 31713 31734 +21
- Misses 2288 2300 +12 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
metcoder95
approved these changes
Jan 26, 2026
Merged
slagiewka
pushed a commit
to slagiewka/undici
that referenced
this pull request
Feb 14, 2026
…s#4772) When a MockAgent had a delayed response and the request was aborted via AbortSignal, the delayed response callback would still fire and attempt to call handler methods after onError had been called. This violated state invariants expected by handlers like DecoratorHandler (used by decompress interceptor). The fix: - Tracks abort state in mockDispatch - Clears pending setTimeout timer when abort is called - Checks abort flag in handleReply before invoking handler callbacks - Properly calls handler.onConnect to register abort callback Fixes nodejs#4693
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 #4693
Problem
When a
MockAgenthad a delayed response (using.delay()) and the request was aborted via anAbortSignal, the delayed response callback would still fire and attempt to call handler methods (onHeaders,onData,onComplete) even after the handler'sonErrorhad been called. This violated state invariants expected by handlers likeDecoratorHandler(used by the decompress interceptor), which has assertions thatonResponseStartshould not be called afteronError.This issue was particularly noticeable when using
MockAgentwith interceptors likedecompress, which would throw an assertion error when receiving a delayed response after abort.Solution
Modified
lib/mock/mock-utils.jsin themockDispatchfunction to:Track abort state: Added
abortedflag andtimerreference to track if the request has been aborted and the delayed response timer.Proper
onConnecthandling: Movedhandler.onConnect?.()call before scheduling the delayed response so the abort callback can be registered.Abort callback cleanup: When the abort callback is invoked:
abortedflagsetTimeouttimer if it existshandler.onError(err)to notify the handlerPrevent late responses: The
handleReplyfunction now checks theabortedflag at two points:If aborted, it silently returns without invoking any handler callbacks.
Tests
Added
test/mock-delayed-abort.jswith three test cases:All existing tests continue to pass.
Checklist