Skip to content

fix(toolkit): preserve unsubscribed results during invalidation - #5347

Closed
rajanpanth wants to merge 1 commit into
reduxjs:masterfrom
rajanpanth:fix/issue-5346-unsubscribed-query-result
Closed

rajanpanth wants to merge 1 commit into
reduxjs:masterfrom
rajanpanth:fix/issue-5346-unsubscribed-query-result

Conversation

@rajanpanth

Copy link
Copy Markdown

Summary

  • preserve the fulfilled result for an unsubscribed query when delayed invalidation runs during fulfillment
  • defer cache removal until the running query settles
  • add deterministic regression coverage for the invalidation race

Fixes #5346.

Problem

A query initiated with subscribe: false can return an uninitialized result when a mutation triggers delayed invalidation while the query is still running.

During the fulfilled dispatch, invalidation sees the cache entry without an active subscription and removes it synchronously. buildInitiate then selects the public result after the cache entry has disappeared, so the initiating caller cannot observe the fulfilled data.

Root cause

The invalidation middleware removes an unsubscribed query result before the running query promise and initiating result-selection flow have settled.

Change

When the affected cache entry still has a running query, defer cleanup until that query settles.

Before cleanup, recheck:

  • the current cache entry still belongs to the original requestId
  • whether a newer request replaced it
  • whether the entry gained an active subscription

An unchanged unsubscribed entry is removed. A newly subscribed entry is refetched, and a newer request is preserved.

Validation

  • focused issue regression — passed
  • race-condition test file — 3/3 passed
  • race-condition test file on Node 22.16 — 3/3 passed
  • Redux Toolkit package suite — 88/88 files and 1,317 tests passed
  • type tests and TypeScript validation — passed
  • targeted ESLint and Prettier — passed
  • production build — passed
  • git show --check — passed

The broad package lint command references an absent packages/toolkit/examples directory. The optional size-limit command also remains blocked by Windows absolute-path resolution. The changed files pass targeted validation and the production build.

Rejected and aborted query results use RTK Query's non-rejecting result promise, so deferred cleanup does not introduce an unhandled rejection. No separate dedicated rejection or abort regression test is included in this change.

Compatibility

No public API, dependency, lockfile, Node support, browser API, or TypeScript declaration change is included.

@codesandbox

codesandbox Bot commented Jul 22, 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 Jul 22, 2026

Copy link
Copy Markdown

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

Name Link
🔨 Latest commit bc36ff0
🔍 Latest deploy log https://app.netlify.com/projects/redux-starter-kit-docs/deploys/6a60f1e8a4d5b40008c0c073
😎 Deploy Preview https://deploy-preview-5347--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.

@aryaemami59 aryaemami59 added the RTK-Query Issues related to Redux-Toolkit-Query label Jul 26, 2026
@pkg-pr-new

pkg-pr-new Bot commented Jul 26, 2026

Copy link
Copy Markdown

Open in StackBlitz

@reduxjs/rtk-codemods

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

@rtk-query/codegen-openapi

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

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

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

@reduxjs/toolkit

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

commit: bc36ff0

@aryaemami59 aryaemami59 changed the title fix(query): preserve unsubscribed results during invalidation fix(toolkit): preserve unsubscribed results during invalidation Jul 26, 2026
@rajanpanth

Copy link
Copy Markdown
Author

Friendly follow-up on this PR 👋

When you have a moment, could you please take a look? I’m happy to rebase, update tests, or make any requested changes.

Thanks!

@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.

Query with subscribed: false returns undefined due to mutation race

3 participants