Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The query gets stuck at fetching status when re-fetched data has cycles #6954

Open
stanislav-halyn opened this issue Feb 22, 2024 · 6 comments

Comments

@stanislav-halyn
Copy link

Describe the bug

The query gets stuck at fetching status when you try to re-fetch data and it has cycles in it.

Your minimal, reproducible example

https://codesandbox.io/p/sandbox/react-query-mfsz4x

Steps to reproduce

  • create a query that returns data with cycles in it
  • re-fetch the query
  • see that the fetchStatus gets stuck at fetching

Expected behavior

The query should change the fetchStatus to idle when re-fetch has been finished.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: MaxOs
  • Browser: Chrome
  • Version: 121.0.6167.184 (Official Build) (x86_64)

Tanstack Query adapter

react-query

TanStack Query version

v5.22.2

TypeScript version

No response

Additional context

As far as I understand, when you turn off structuralSharing for the query - the issue will go away.
After some digging, I found that default structuralSharing functionality is to call replaceEqualDeep function. And when there are cycles in the data, this function goes into infinite loop because it recursively calls itself on each data property.

@stanislav-halyn
Copy link
Author

I will prepare a PR since the fix seems quite straightforward: add a visited WeakSet to check if we have processed the item already.

@TkDodo
Copy link
Collaborator

TkDodo commented Feb 24, 2024

maybe we need to make it more obvious in the docs, but for structuralSharing to work, data must be JSON-serializable.

Screenshot 2024-02-24 at 11 24 34

if that's not the case, you can:

  • turn off structuralSharing by setting it to false
  • provide your own method to structuralSharing to share data yourself.

add a visited WeakSet to check if we have processed the item already.

I think this is something you would do in a custom structuralSharing function. It's a conscious decision to only work with JSON serializable data on our end.

@TkDodo TkDodo closed this as not planned Won't fix, can't repro, duplicate, stale Feb 24, 2024
@TkDodo
Copy link
Collaborator

TkDodo commented Feb 24, 2024

however, I think the query should go into error state if that happens. I think that might be the real issue ?

@stanislav-halyn
Copy link
Author

@TkDodo yeah, I think it should, because it took me some time to figure out what was the issue with the query, since I didn't know my data wasn't JSON-serializable

@NikSimonov
Copy link

NikSimonov commented May 17, 2024

got stuck with this problem for a long time too, would be good to get error in this case

@TkDodo
Copy link
Collaborator

TkDodo commented May 17, 2024

however, I think the query should go into error state if that happens. I think that might be the real issue ?

okay, does someone want to contribute this ? I think a try/catch around here:

this.setData(data)

  • calling onError in the catch should do it

@TkDodo TkDodo reopened this May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants