fix(query-core): propagate AbortSignal reason in infiniteQueryBehavior fetchPage cancellation #10476
main fromrefs/pull/10476/head·merged by TkDodo View on GitHubSummary#
Fixes #10475
Problem#
In infiniteQueryBehavior.ts, the internal fetchPage helper function calls Promise.reject() without any argument when a query cancellation is detected via the cancelled flag (which is set by the AbortSignal abort event listener):
// Before (bug)
const fetchPage = async (data, param, previous) => {
if (cancelled) {
return Promise.reject() // rejects with undefined!
}
// ...
}
When query.cancel() → abortController.abort() is called, the abort signal fires and sets cancelled = true. On the next iteration of the multi-page fetch loop, fetchPage rejects with undefined instead of the proper AbortError that is set as context.signal.reason.
Fix#
Propagate context.signal.reason as the rejection value, which is a DOMException with name "AbortError" when AbortController.abort() is called without an explicit reason (per the AbortSignal spec):
// After (fix)
const fetchPage = async (data, param, previous) => {
if (cancelled) {
return Promise.reject(context.signal.reason) // proper AbortError DOMException
}
// ...
}
Impact#
This fixes the following issues that occur when an infinite query is cancelled mid-pagination:
- Custom
retryfunctions receivingundefinedinstead of anAbortError QueryCache.onErrorcallbacks receivingundefinedas the error argument- Error boundaries via
throwOnErrorshowing"undefined"errors
Testing#
The fix is minimal and targeted. The change ensures that when the AbortSignal fires during a multi-page infinite query fetch, the proper DOMException{ name: "AbortError" } is propagated as the rejection reason, consistent with the Web AbortController specification.
Summary by CodeRabbit#
Bug Fixes
- Cancellation now surfaces the underlying abort reason when an in-flight operation is cancelled, improving error diagnostics and debugging.
Tests
- Added a unit test to validate that cancellation propagates the abort reason and that fetch behavior correctly rejects with that reason.
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: query-core
TkDodo reviewed
this looks good, but can you please add the steps-to-reproduce from your issue as a test case? Ideally, that test fails on main but works with your PR. Thanks.
Thanks for the review, @TkDodo! I'll add a test case that reproduces the issue from #10475.
The test will demonstrate that when a query is aborted, the retry function and error handlers receive the proper
AbortErrorreason (i.e.,context.signal.reason) instead ofundefined. I'll push the test shortly — it will fail onmainand pass with this PR's change.View your CI Pipeline Execution ↗ for commit 4f818c80ee0660ce85154c068e911f889be2c745
Command Status Duration Result nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded 5m 38s View ↗ nx run-many --target=build --exclude=examples/*...✅ Succeeded 2s View ↗
☁️ Nx Cloud last updated this comment at
2026-04-25 16:59:31UTCMore templates
- @tanstack/query-example-angular-auto-refetching
- @tanstack/query-example-angular-basic
- @tanstack/query-example-angular-basic-persister
- @tanstack/query-example-angular-devtools-panel
- @tanstack/query-example-angular-infinite-query-with-max-pages
- @tanstack/query-example-angular-optimistic-updates
- @tanstack/query-example-angular-pagination
- @tanstack/query-example-angular-query-options-from-a-service
- @tanstack/query-example-angular-router
- @tanstack/query-example-angular-rxjs
- @tanstack/query-example-angular-simple
- @tanstack/query-example-preact-simple
- @tanstack/query-example-react-algolia
- @tanstack/query-example-react-auto-refetching
- @tanstack/query-example-react-basic
- @tanstack/query-example-react-basic-graphql-request
- @tanstack/query-example-chat
- @tanstack/query-example-react-default-query-function
- @tanstack/query-example-react-devtools-panel
- @tanstack/query-example-eslint-legacy
- @tanstack/query-example-eslint-plugin-demo
- @tanstack/query-example-react-infinite-query-with-max-pages
- @tanstack/query-example-react-load-more-infinite-scroll
- @tanstack/query-example-react-nextjs
- @tanstack/query-example-react-nextjs-app-prefetching
- @tanstack/query-example-nextjs-suspense-streaming
- @tanstack/query-example-react-offline
- @tanstack/query-example-react-optimistic-updates-cache
- @tanstack/query-example-react-optimistic-updates-ui
- @tanstack/query-example-react-pagination
- @tanstack/query-example-react-playground
- @tanstack/query-example-react-prefetching
- @tanstack/query-example-react-react-native
- @tanstack/query-example-react-router
- @tanstack/query-example-react-rick-morty
- @tanstack/query-example-react-shadow-dom
- @tanstack/query-example-react-simple
- @tanstack/query-example-react-star-wars
- @tanstack/query-example-react-suspense
- @tanstack/query-example-solid-astro
- @tanstack/query-example-solid-basic
- @tanstack/query-example-solid-basic-graphql-request
- @tanstack/query-example-solid-default-query-function
- @tanstack/query-example-solid-offline
- @tanstack/query-example-solid-simple
- @tanstack/query-example-solid-start-streaming
- @tanstack/query-example-svelte-auto-refetching
- @tanstack/query-example-svelte-basic
- @tanstack/query-example-svelte-load-more-infinite-scroll
- @tanstack/query-example-svelte-optimistic-updates
- @tanstack/query-example-svelte-playground
- @tanstack/query-example-svelte-simple
- @tanstack/query-example-svelte-ssr
- @tanstack/query-example-svelte-star-wars
- @tanstack/query-example-vue-2.6-basic
- @tanstack/query-example-vue-2.7-basic
- @tanstack/query-example-vue-basic
- @tanstack/query-example-vue-dependent-queries
- @tanstack/query-example-vue-nuxt3
- @tanstack/query-example-vue-persister
- @tanstack/query-example-vue-simple
@tanstack/angular-query-experimental
npm i https://pkg.pr.new/@tanstack/angular-query-experimental@10476@tanstack/eslint-plugin-query
npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@10476@tanstack/preact-query
npm i https://pkg.pr.new/@tanstack/preact-query@10476@tanstack/preact-query-devtools
npm i https://pkg.pr.new/@tanstack/preact-query-devtools@10476@tanstack/preact-query-persist-client
npm i https://pkg.pr.new/@tanstack/preact-query-persist-client@10476@tanstack/query-async-storage-persister
npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@10476@tanstack/query-broadcast-client-experimental
npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@10476@tanstack/query-core
npm i https://pkg.pr.new/@tanstack/query-core@10476@tanstack/query-devtools
npm i https://pkg.pr.new/@tanstack/query-devtools@10476@tanstack/query-persist-client-core
npm i https://pkg.pr.new/@tanstack/query-persist-client-core@10476@tanstack/query-sync-storage-persister
npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@10476@tanstack/react-query
npm i https://pkg.pr.new/@tanstack/react-query@10476@tanstack/react-query-devtools
npm i https://pkg.pr.new/@tanstack/react-query-devtools@10476@tanstack/react-query-next-experimental
npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@10476@tanstack/react-query-persist-client
npm i https://pkg.pr.new/@tanstack/react-query-persist-client@10476@tanstack/solid-query
npm i https://pkg.pr.new/@tanstack/solid-query@10476@tanstack/solid-query-devtools
npm i https://pkg.pr.new/@tanstack/solid-query-devtools@10476@tanstack/solid-query-persist-client
npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@10476@tanstack/svelte-query
npm i https://pkg.pr.new/@tanstack/svelte-query@10476@tanstack/svelte-query-devtools
npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@10476@tanstack/svelte-query-persist-client
npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@10476@tanstack/vue-query
npm i https://pkg.pr.new/@tanstack/vue-query@10476@tanstack/vue-query-devtools
npm i https://pkg.pr.new/@tanstack/vue-query-devtools@10476commit:
4f818c8🧹 Nitpick comments (1)
packages/query-core/src/__tests__/infiniteQueryBehavior.test.tsx (1)
336-374: Test correctly validates the fix; consider a brief comment on the lazy signal access.The test reasoning is sound: with two existing pages in
state.data, the refetch path invokesfetchPagetwice. The first call letsqueryFnrun, abort the controller, and resolve withpageParam— which is whyqueryFnis asserted to be called exactly once. The secondfetchPagethen short-circuits on thecancelledflag and rejects withsignal.reason, which is the behavior this PR introduces.Two small notes:
- The whole test hinges on
void signal.abortedtriggering registration of the abort listener insideaddConsumeAwareSignal(the listener is only attached on first access ofcontext.signal). Without that line,cancelledwould never flip totrueand the test would hang/fail for the wrong reason. A one-line comment would save future readers from wondering why a seemingly dead expression is there. - Since
signal.reasonbecomes a stable reference onceabort()fires,.rejects.toBe(...)is an appropriately strict assertion (good — it would catch a regression that wraps/rethrows the reason).
📝 Optional: clarify the intent of the signal access
const queryFn = vi.fn().mockImplementation(({ pageParam, signal }) => { + // Access `signal` so `addConsumeAwareSignal` registers the abort listener + // that flips the internal `cancelled` flag in fetchPage. void signal.aborted if (pageParam === 1) { abortController.abort() } return pageParam })🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/query-core/src/__tests__/infiniteQueryBehavior.test.tsx` around lines 336 - 374, Add a one-line test comment explaining the intentional lazy access of the AbortSignal (the `void signal.aborted` expression) so future readers know it's required to trigger `addConsumeAwareSignal`'s listener attachment; place the comment immediately above the `void signal.aborted` line inside the test "should surface the abort reason when cancellation happens between refetched pages" next to the `queryFn` mock setup to clarify the purpose of that seemingly no-op access.🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed. Nitpick comments: In `@packages/query-core/src/__tests__/infiniteQueryBehavior.test.tsx`: - Around line 336-374: Add a one-line test comment explaining the intentional lazy access of the AbortSignal (the `void signal.aborted` expression) so future readers know it's required to trigger `addConsumeAwareSignal`'s listener attachment; place the comment immediately above the `void signal.aborted` line inside the test "should surface the abort reason when cancellation happens between refetched pages" next to the `queryFn` mock setup to clarify the purpose of that seemingly no-op access.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID:
5c3d3549-9ed0-4462-a639-43b608ee75eb📥 Commits
Reviewing files that changed from the base of the PR and between 2163c11c8066021553bd8050dedba59c654e9a0e and 4f818c80ee0660ce85154c068e911f889be2c745.
📒 Files selected for processing (1)
packages/query-core/src/__tests__/infiniteQueryBehavior.test.tsx
- The whole test hinges on
TkDodo merged this672c319
TkDodo closed this
Sign in to join this conversation.
📝 Walkthrough
Walkthrough#
The cancellation path in
fetchPage(infiniteQueryBehavior) now rejects withcontext.signal.reasonwhen aborted. A unit test was added to assert that a cancelled in-flight infinite query surfaces the AbortSignal reason instead of an undefined rejection.Changes#
packages/query-core/src/infiniteQueryBehavior.tscontext.signal.reasoninstead of rejecting without a value.packages/query-core/src/__tests__/infiniteQueryBehavior.test.tsxAbortSignal, triggers cancellation during a page fetch, and asserts the fetch promise rejects withsignal.reasonand thatqueryFncall counts are expected.Estimated code review effort#
🎯 2 (Simple) | ⏱️ ~10 minutes
Poem#
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)#
✅ Passed checks (4 passed)
#10475: Promise.reject() now includes context.signal.reason, and a test validates the AbortSignal reason is properly propagated.✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share