Skip to content

Commit 2d68e8a

Browse files
authored
fix: guard against AbortSignal-aborted requests in response handler (#2977)
When a request's AbortSignal fires, @mswjs/interceptors immediately transitions the request to an error state. If nock's response callback runs after that transition (e.g. because a reply delay was configured), calling controller.respondWith() throws an InterceptorError ("the request has already been handled"). Add a signal.aborted check before respondWith() so that a late-firing response callback exits early rather than throwing. Fixes #2949
1 parent de5450c commit 2d68e8a

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

‎lib/intercept.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,13 @@ function activate() {
398398
globalEmitter.emit('no match', nockRequest)
399399
} else {
400400
nockRequest.on('response', nockResponse => {
401+
// Guard against the case where the AbortSignal fired before nock
402+
// finished building the response. In that scenario the interceptor
403+
// has already transitioned the request to an error state and calling
404+
// controller.respondWith() would throw an InterceptorError.
405+
if (mswRequest.signal.aborted) {
406+
return
407+
}
401408
const response = createResponse(nockResponse, mswRequest.signal)
402409
controller.respondWith(response)
403410
})

‎tests/test_abort_signal.js‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,34 @@ describe('When `AbortSignal` is used', () => {
174174
})
175175
.catch(error => done(error))
176176
})
177+
178+
it('does not throw when AbortSignal timeout fires before the mocked response is delivered', async () => {
179+
// Regression test for https://github.com/nock/nock/issues/2949.
180+
//
181+
// The @mswjs/interceptors upgrade in v14.0.11 changed AbortSignal handling:
182+
// a timed-out request transitions to readyState ERROR immediately when the
183+
// signal fires. When nock's response-ready callback ran after that
184+
// transition it would call controller.respondWith() on an already-errored
185+
// request and throw an uncaught InterceptorError
186+
// ("the request has already been handled").
187+
//
188+
// Use a 50 ms reply delay with a 1 ms signal timeout so the signal is
189+
// guaranteed to fire well before nock delivers the response, ensuring the
190+
// guard in the response callback is exercised.
191+
const signal = AbortSignal.timeout(1)
192+
const scope = nock('http://example.test')
193+
.get('/')
194+
.delay(50) // fires after the signal has already aborted
195+
.reply(200, 'OK')
196+
197+
const error = await makeRequest('http://example.test/', {
198+
signal,
199+
}).catch(error => error)
200+
201+
// The request must abort cleanly — no unhandled InterceptorError
202+
expect(error).to.have.property('name', 'AbortError')
203+
expect(error).to.have.property('code', 'ABORT_ERR')
204+
205+
scope.done()
206+
})
177207
})

0 commit comments

Comments
 (0)