Skip to content

Query with subscribed: false returns undefined due to mutation race #5346

Closed

Description

@emersion

Steps:

  1. Create an API with a query and a mutation sharing the same cache tag
  2. Start a request for the query with subscribe: false
  3. Start a request for the mutation
  4. If the mutation completes right before the query, the query returns undefined, breaking its API contract (types claim that undefined is never returned)

Here is a minimal reproducer: https://gist.github.com/emersion/1459bcabb66c5bb2c6c72f32f07fce3f

To run it:

git clone https://gist.github.com/1459bcabb66c5bb2c6c72f32f07fce3f.git rtk-query-unsubrace
cd rtk-query-unsubrace
npm install
npm start

The delay might need to be adjusted depending on the host machine. On my machine, the hardcoded delay is enough to hit the race consistently. If you can't reproduce with the hardcoded delay, use the random delay and let the script run for a while.

Setting subscribe: true and calling unsubscribe() after awaiting the query works around the bug. Calling unsubscribe() any earlier is not enough.

Activity

  1. added
    RTK-Query
    on Jul 26, 2026
  2. added theissue type on Jul 26, 2026
  3. changed the title [-]Query with `subscribed: false` returns undefined due to mutation race[/-] [+]Query with `subscribed: false` returns `undefined` due to mutation race[/+] on Nov 2, 2026
  4. emersion commented on Nov 6, 2026

    @emersion
    ContributorAuthor

    Thanks for the fix @markerikson!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    RTK-Query

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions