Skip to content
Source
Sign inSign up
TanStack/query

leak in query garbage collection #2946

Send to my agent
ClosedTkDodo opened this issue 1 comment
TkDodocommented

Describe the bug

Queries that should be garbage collected because the cacheTime has elapsed are kept in memory forever if the query was fetching when the last observer was removed.

To Reproduce https://codesandbox.io/s/youthful-neumann-rh8t6?file=/src/index.js

note:

  • cacheTime is 2 seconds
  • the query takes 5 seconds to fetch
  • it's best to wait until the query has loaded once, then trigger a refetch via window focus and click the button which will unmount the query while the fetch is happening.

Expected behavior queries to be garbage collected correctly

Additional context

I think the problem is that optionalRemove, once it determines that it has no observers but doesn't remove the query because it's still fetching doesn't restart the timer:

https://github.com/tannerlinsley/react-query/blob/c1ae82ba188fd5abda5e256cac070145e5941447/src/core/query.ts#L216-L220

  1. TkDodo added labelsbug

  2. TkDodo closed this

  3. :tada: This issue has been resolved in version 3.33.3 :tada:

    The release is available on:

    Your semantic-release bot :package::rocket:

  4. tannerlinsley added labelsreleased

Sign in to join this conversation.