Repository navigation
Conversation
…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`.
Review or Edit in CodeSandboxOpen the branch in Web Editor • VS Code • Insiders |
✅ Deploy Preview for redux-starter-kit-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
@reduxjs/rtk-codemods
@rtk-query/codegen-openapi
@rtk-query/graphql-request-base-query
@reduxjs/toolkit
commit: |
…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.
5 tasks
unwrap when the cache entry was removed mid-requestunwrap when the cache entry was removed mid-request
Collaborator
|
Superseded by #5587 |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 #4833.
Also covers #5173, though it rejects rather than returning data (see below).
await dispatch(api.endpoints.x.initiate(arg)).unwrap()resolvesundefinedwhen the query's cache entry is removed while the request is still running. Same for.unwrap()on auseLazyQuerytrigger. The type says it's the data, so TS won't catch it.useLazyQuerymethod call returnsundefinedwhen called from an unmounted component #5173: lazy query withkeepUnusedDataFor: 0, the component unmounts right after triggering, andawait getPost().unwrap()givesundefined. A second user in that thread hit it after upgrading, by navigating away mid-request. Aborting a running query on cache removal came in with Abort pending requests if the cache entry is removed #5061 (v2.9.0): cacheCollection.ts#L194initiatereturnsundefinedwhen unwrapping afterresetApiState#4833:resetApiState()whileinitiate().unwrap()is pending. Since v2.9.2 the reset aborts outstanding requests, and the abort does reach the thunk, butunwrap()still resolvesundefined.Query
unwrap()answers from the cache entry (buildInitiate.ts#L471). Once the entry is gone it reads as uninitialized, with no error to throw anddataundefined, and the thunk's own result, which has the abort, is never looked at.Fix
In
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 withConditionError(the thing #1786 took out of queryunwrap). Mutationunwrapalready 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 forundefinedafterunwrap()will get a throw instead.Tests
Three in
cacheCollection.test.ts:resetApiStatewith one caller,resetApiStatewith two callers of the same arg (both get the AbortError, neitherConditionError), andkeepUnusedDataFor: 0unsubscribed while running. All three fail on master:pnpm testinpackages/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.