fix: MockAgent delayed response with AbortSignal (#4693) #4772
+172
−2
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