Skip to content
Source
Sign inSign up
TanStack/query

Query remains active even without any listeners #7711

Send to my agent
ClosedTPReal(GitHub profile, opens in a new tab) opened this issue 3 comments

Describe the bug

I have a component that calls createQuery. This is the only listener of that query. When the component is removed from the page, the query should become inactive as its only listener got cleaned up, and this is usually the case. However, I found some scenarios that cause the query to still be active after the component is removed, and I'm quite sure this is incorrect behaviour.

Your minimal, reproducible example

https://stackblitz.com/edit/tanstack-query-1w7g6n?file=src%2Findex.tsx

Steps to reproduce

  1. Open the console and see "Fetching" printed every second - it's the Comp component fetching, and invalidations being fired every second.
  2. See that clicking any of the buttons marked [good] destroys the component and stops the query - invalidations still occur, but the query is inactive and does not fetch. The query only fetches when the component is on the page - correct.
  3. Refresh the page each time after testing to be sure you have the clean state.
  4. Now test one of the buttons marked [bad] - a reversed order of events or usage of a microtask breaks the query - it is still active and fetching despite the component being destroyed.

Expected behavior

Expected: When the component is removed and its onCleanup has run, the query is inactive and invalidations don't cause it to fetch. Actual: It depends.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

Windows 11, Chrome 126.0.6478.127

Tanstack Query adapter

solid-query

TanStack Query version

5.50.2

TypeScript version

5.5.3

Additional context

One of the ways to get this problem is using solid-router and navigating at the same moment as invalidation (any order I think).

I spent a lot of time debugging this and figured out that the problem is visible around this code:

  const createClientSubscriber = () => {
    const obs = observer();
    return obs.subscribe((result) => {
      observerResult = result;
      queueMicrotask(() => refetch());
    });
  };

The order of events I observed:

  • The invalidation causes the query to start fetching again. The obs.subscribe callback runs with the result with isFetching.
  • The component is removed, and the onCleanup in the query runs, removing the listener, and calling destroy on the query - now for a moment it is inactive.
  • The microtask scheduled in the code above runs, so the resource refetches. The code of the resource adds again a listener to this already inactive query. This listener will never be removed as onCleanup was already called.
  1. @ardeora please have a look here

  2. TkDodo added labelspackage: solid-query

  3. Hello @TPReal ! Thank you so much for the detailed reproduction. This bug has been fixed in newer versions of solid-query (v5.56.1+)

    Here is the same reproduction working as expected. I have also included the devtools to show that the observers are being cleaned up without any issue. Please let me know if you still run into any issues. https://stackblitz.com/edit/tanstack-query-1bqjjq?file=package.json,src%2Findex.tsx

  4. Thanks a lot! I don't see any problems on the updated version. I spent at least 8h debugging this issue, appearing in two different places in our app, so I'm really happy it's gone.

    (You got the version number in your comment wrong.)

Sign in to join this conversation.