-
-
Notifications
You must be signed in to change notification settings - Fork 66
Fix: canceled requests no longer propagate errors to deduplicated requests #1172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| import assert from 'node:assert'; | ||
| import { describe, it } from 'node:test'; | ||
| import { setTimeout } from 'node:timers/promises'; | ||
| import Axios from 'axios'; | ||
| import type { CacheAxiosResponse, InternalCacheRequestConfig } from '../../src/cache/axios.js'; | ||
| import { setupCache } from '../../src/cache/create.js'; | ||
|
|
||
| describe('Aborted Request Handling', () => { | ||
| it('Second request should succeed after first request is aborted', async () => { | ||
| const instance = Axios.create({}); | ||
| const axios = setupCache(instance, { | ||
| interpretHeader: false | ||
| }); | ||
|
|
||
| let requestCount = 0; | ||
|
|
||
| // Mock adapter that simulates a network request | ||
| axios.defaults.adapter = async (config: InternalCacheRequestConfig) => { | ||
| requestCount++; | ||
|
|
||
| // Simulate network delay | ||
| await setTimeout(100); | ||
|
|
||
| return { | ||
| data: { success: true }, | ||
| status: 200, | ||
| statusText: 'OK', | ||
| headers: {}, | ||
| config, | ||
| request: { config } | ||
| }; | ||
| }; | ||
|
|
||
| // Create an AbortController | ||
| const abortController = new AbortController(); | ||
|
|
||
| // Start the first request with abort signal | ||
| const req1Promise = axios.get('https://test.com/products', { | ||
| signal: abortController.signal | ||
| }); | ||
|
|
||
| // Abort the request after a short delay | ||
| setTimeout(10).then(() => { | ||
| abortController.abort(); | ||
| }); | ||
|
|
||
| // Wait for the abort to happen | ||
| let req1Error: any; | ||
| try { | ||
| await req1Promise; | ||
| } catch (error) { | ||
| req1Error = error; | ||
| } | ||
|
|
||
| // First request should be aborted | ||
| assert.ok(req1Error); | ||
| assert.equal(req1Error.code, 'ERR_CANCELED'); | ||
|
|
||
| // Second request with same parameters should succeed | ||
| const req2 = await axios.get('https://test.com/products'); | ||
|
|
||
| assert.equal(req2.data.success, true); | ||
| assert.equal(req2.status, 200); | ||
|
|
||
| // The second request should have made a network call since the first was aborted | ||
| assert.equal(requestCount, 2); | ||
| }); | ||
|
|
||
| it('Second request made immediately after aborting should succeed (issue reproduction)', async () => { | ||
| const instance = Axios.create({}); | ||
| const axios = setupCache(instance, { | ||
| interpretHeader: false, | ||
| debug: (_msg) => { | ||
| // Uncomment to see debug logs like in the issue | ||
| // console.log(JSON.stringify(msg, null, 2)); | ||
| } | ||
| }); | ||
|
|
||
| let requestCount = 0; | ||
|
|
||
| // Mock adapter that simulates a network request | ||
| axios.defaults.adapter = async (config: InternalCacheRequestConfig) => { | ||
| requestCount++; | ||
|
|
||
| // Simulate network delay | ||
| await setTimeout(50); | ||
|
|
||
| return { | ||
| data: { success: true, requestId: requestCount }, | ||
| status: 200, | ||
| statusText: 'OK', | ||
| headers: {}, | ||
| config, | ||
| request: { config } | ||
| }; | ||
| }; | ||
|
|
||
| // Reproduce the exact scenario from the issue | ||
| const abortController = new AbortController(); | ||
| const req1 = axios.get('https://dummyjson.com/products', { | ||
| signal: abortController.signal | ||
| }); | ||
|
|
||
| // After 10ms, abort the first request AND make the second request immediately | ||
| // This is the key part - both happen in the same setTimeout | ||
| const req2Promise = new Promise<CacheAxiosResponse>((resolve, reject) => { | ||
| setTimeout(10).then(() => { | ||
| abortController.abort(); | ||
| const req2 = axios.get('https://dummyjson.com/products'); | ||
| req2.then(resolve).catch(reject); | ||
| }); | ||
| }); | ||
|
|
||
| // First request should fail with abort error | ||
| await assert.rejects(req1, { code: 'ERR_CANCELED' }); | ||
|
|
||
| // Second request should succeed (this is where the bug would show) | ||
| const req2 = await req2Promise; | ||
| assert.equal(req2.data.success, true); | ||
| assert.equal(req2.status, 200); | ||
| }); | ||
|
|
||
| it('Multiple concurrent requests after aborted request should all succeed', async () => { | ||
| const instance = Axios.create({}); | ||
| const axios = setupCache(instance, { | ||
| interpretHeader: false | ||
| }); | ||
|
|
||
| let requestCount = 0; | ||
|
|
||
| // Mock adapter | ||
| axios.defaults.adapter = async (config: InternalCacheRequestConfig) => { | ||
| requestCount++; | ||
| await setTimeout(100); | ||
|
|
||
| return { | ||
| data: { success: true, count: requestCount }, | ||
| status: 200, | ||
| statusText: 'OK', | ||
| headers: {}, | ||
| config, | ||
| request: { config } | ||
| }; | ||
| }; | ||
|
|
||
| // First request with abort | ||
| const abortController = new AbortController(); | ||
| const req1Promise = axios.get('https://test.com/api', { | ||
| signal: abortController.signal | ||
| }); | ||
|
|
||
| setTimeout(10).then(() => { | ||
| abortController.abort(); | ||
| }); | ||
|
|
||
| await assert.rejects(req1Promise, { code: 'ERR_CANCELED' }); | ||
|
|
||
| // Make multiple concurrent requests after abort | ||
| const [req2, req3, req4] = await Promise.all([ | ||
| axios.get('https://test.com/api'), | ||
| axios.get('https://test.com/api'), | ||
| axios.get('https://test.com/api') | ||
| ]); | ||
|
|
||
| // All should succeed | ||
| assert.equal(req2.data.success, true); | ||
| assert.equal(req3.data.success, true); | ||
| assert.equal(req4.data.success, true); | ||
|
|
||
| // First request was aborted (1 call) | ||
| // Second batch should be deduplicated (1 call) | ||
| // Total: 2 calls | ||
| assert.equal(requestCount, 2); | ||
|
|
||
| // The second and third should be cached from the second request | ||
| assert.equal(req2.cached, false); | ||
| assert.ok(req3.cached); | ||
| assert.ok(req4.cached); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test name "Cancelled deferred propagates error to all waiting requests" is now misleading since the test validates that waiting requests should succeed (not receive the error). Consider updating the test name to reflect the new behavior, such as "Cancelled request does not propagate error to waiting deduplicated requests" or "Waiting requests succeed after first request is cancelled".