Skip to content

fix(toolkit): reject query unwrap when the cache entry was removed mid-request - #5474

Closed
kkatalz wants to merge 2 commits into
reduxjs:masterfrom
kkatalz:fix/reject-query-unwrap-when-the-cache-entry
Closed

kkatalz wants to merge 2 commits into
reduxjs:masterfrom
kkatalz:fix/reject-query-unwrap-when-the-cache-entry

Conversation

@kkatalz

@kkatalz kkatalz commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

Fixes #4833.
Also covers #5173, though it rejects rather than returning data (see below).

await dispatch(api.endpoints.x.initiate(arg)).unwrap() resolves undefined when the query's cache entry is removed while the request is still running. Same for .unwrap() on a useLazyQuery trigger. The type says it's the data, so TS won't catch it.

Query unwrap() answers from the cache entry (buildInitiate.ts#L471). Once the entry is gone it reads as uninitialized, with no error to throw and data undefined, and the thunk's own result, which has the abort, is never looked at.

Fix

In unwrap():

if (result.isUninitialized) {
  return (runningQuery ?? thunkResult).unwrap()
}

If the entry is still there it's the same code path as before. runningQuery ?? is there so a deduped caller unwraps the original request instead of its own condition-skipped thunk, which would reject with ConditionError (the thing #1786 took out of query unwrap). Mutation unwrap already unwraps the thunk itself.

This is a behaviour change for #5173: the reporter wanted the data, but the removal aborts the request, so there isn't any. unwrap() now rejects with the AbortError, and code that checks for undefined after unwrap() will get a throw instead.

Tests

Three in cacheCollection.test.ts: resetApiState with one caller, resetApiState with two callers of the same arg (both get the AbortError, neither ConditionError), and keepUnusedDataFor: 0 unsubscribed while running. All three fail on master:

     × endpoint keepUnusedDataFor: 0, unsubscribed while running: unwrap rejects 52ms
     × unwrap rejects for a query aborted by resetApiState 9ms
     × unwrap rejects for every caller of a query aborted by resetApiState 7ms
AssertionError: promise resolved "undefined" instead of rejecting

pnpm test in packages/toolkit: 89 files, 1367 passed, 2 todo, no type errors. With only the fix reverted, the same run fails exactly those three. tsc -p tsconfig.test.json, oxfmt and oxlint are clean.

…d mid-request

When a query's cache entry is removed while its request is running
(`resetApiState`, or `keepUnusedDataFor` removal after the last
unsubscribe), the request is aborted and the entry is gone, so `unwrap()`
read an uninitialized entry and resolved `undefined`. It now settles with
the result of the request it waited on, which rejects with the abort
error. A deduplicated caller follows the running query rather than its
own condition-skipped thunk, so it does not reject with `ConditionError`.
@codesandbox

codesandbox Bot commented Oct 17, 2026

Copy link
Copy Markdown

Review or Edit in CodeSandbox

Open the branch in Web Editor • VS Code • Insiders

Open Preview

@netlify

netlify Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for redux-starter-kit-docs ready!

Name Link
🔨 Latest commit 7faa38b
🔍 Latest deploy log https://app.netlify.com/projects/redux-starter-kit-docs/deploys/6ad3b9ffbe0db24fff7fb224
😎 Deploy Preview https://deploy-preview-5474--redux-starter-kit-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@pkg-pr-new

pkg-pr-new Bot commented Oct 17, 2026

Copy link
Copy Markdown

Open in StackBlitz

@reduxjs/rtk-codemods

npm i https://pkg.pr.new/@reduxjs/rtk-codemods@9c913b4 -D
yarn add https://pkg.pr.new/@reduxjs/rtk-codemods@9c913b4.tgz -D
pnpm add https://pkg.pr.new/@reduxjs/rtk-codemods@9c913b4.tgz -D
bun add https://pkg.pr.new/@reduxjs/rtk-codemods@9c913b4.tgz -D

@rtk-query/codegen-openapi

npm i https://pkg.pr.new/@rtk-query/codegen-openapi@9c913b4 -D
yarn add https://pkg.pr.new/@rtk-query/codegen-openapi@9c913b4.tgz -D
pnpm add https://pkg.pr.new/@rtk-query/codegen-openapi@9c913b4.tgz -D
bun add https://pkg.pr.new/@rtk-query/codegen-openapi@9c913b4.tgz -D

@rtk-query/graphql-request-base-query

npm i https://pkg.pr.new/@rtk-query/graphql-request-base-query@9c913b4 -D
yarn add https://pkg.pr.new/@rtk-query/graphql-request-base-query@9c913b4.tgz -D
pnpm add https://pkg.pr.new/@rtk-query/graphql-request-base-query@9c913b4.tgz -D
bun add https://pkg.pr.new/@rtk-query/graphql-request-base-query@9c913b4.tgz -D

@reduxjs/toolkit

npm i https://pkg.pr.new/@reduxjs/toolkit@9c913b4 -D
yarn add https://pkg.pr.new/@reduxjs/toolkit@9c913b4.tgz -D
pnpm add https://pkg.pr.new/@reduxjs/toolkit@9c913b4.tgz -D
bun add https://pkg.pr.new/@reduxjs/toolkit@9c913b4.tgz -D

commit: 9c913b4

…r result test

The test ends while its request is still running, and the store reset in
afterEach aborts it. With query `unwrap` now rejecting when the cache entry
is removed mid-request, the uncaught `await res.unwrap()` in the component
showed up as two unhandled AbortErrors and failed the run in CI.
@aryaemami59 aryaemami59 added the RTK-Query Issues related to Redux-Toolkit-Query label Nov 2, 2026
@aryaemami59 aryaemami59 changed the title fix(rtk-query): reject query unwrap when the cache entry was removed mid-request fix(toolkit): reject query unwrap when the cache entry was removed mid-request Nov 2, 2026
@markerikson

Copy link
Copy Markdown
Collaborator

Superseded by #5587

@markerikson markerikson closed this Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

RTK-Query Issues related to Redux-Toolkit-Query

Projects

None yet

Development

Successfully merging this pull request may close these issues.

initiate returns undefined when unwrapping after resetApiState

3 participants