apollographql / apollo-client

:rocket:  A fully-featured, production ready caching GraphQL client for every UI framework and GraphQL server.
https://apollographql.com/client
MIT License
19.34k stars 2.65k forks source link

clearStore causes a crash when there is an active query #11846

Closed ach5910 closed 1 month ago

ach5910 commented 4 months ago

Issue Description

If client.clearStore is called while a query response is pending, it's causing a crashing at this location https://github.com/apollographql/apollo-client/blob/main/src/react/hooks/useQuery.ts#L635.

Screenshot 2024-05-15 at 9 05 37 AM

I am using

and here's how I am initializing my client

persistCacheSync({
  cache,
  storage: new MMKVWrapper(mmkvStorage),
  persistenceMapper,
  trigger: "write",
  debug: __DEV__,
});

const persistLink = createPersistLink();

const client = new ApolloClient({
  cache,
  link: from([
    persistLink,
    sentryBreadcrumbsLink,
    retryLink,
    authLink,
    tokenRefreshLink,
    errorLink,
    httpLink,
  ]),
});

I've also tried using resetStore, but I get the same error. Any feedback regarding a potential cause is greatly appreciated.

Link to Reproduction

N/A

Reproduction Steps

No response

@apollo/client version

3.9.11

bignimbus commented 4 months ago

Hi @ach5910 👋🏻 thanks for letting us know! I'll chat with the team soon to see if there's anything that immediately jumps out as a bug in Apollo Client. Are you able to reproduce this issue in a clean app (e.g. without the other libraries)?

Nehal-Zeller commented 4 months ago

Facing the same error, is there any way we can kill all pending active queries other than clearStore?

ach5910 commented 4 months ago

@bignimbus Unfortunately I couldn't reproduce the bug in a sandboxed app due to the complexity of mirroring our production app's infrastructure. I know that not much help in diagnosing the problem, but I was really hoping I was missing something basic and I'd get a "RTFD" response.

@Nehal-Zeller I found a solution by calling client.cache.reset() instead of client.clearStore() or client.resetStore(). I believe any active requests will update the cache if returned after resetting, but it beats having the app crash. Hope this helps!

mikolajadamowicz commented 2 months ago

I am facing the same issue. I believe that client.cache.reset() is not killing the pending query thus it will be written to cache once the response is returned.

It's worth to mention that i am encountering this error randomly on react native. I can't easily reproduce that.

DerekWang98 commented 1 month ago

Am also facing this issue for a while now. Upgraded to @apollo/client@3.11.1 and am still able to reproduce this error by running a query and then calling client.clearStore().

jerelmiller commented 1 month ago

@DerekWang98 are you seeing the same stack trace and error as the issue description (unexpected), or are you seeing an error with a message of Store reset while query was in flight (not completed in link chain). If the latter, this is expected behavior, but if the former, then a reproduction of the issue would be greatly appreciated.

DerekWang98 commented 1 month ago

Hey @jerelmiller, I get the same error stack trace error in node_modules/@apollo/client/react/hooks/useQuery.js

To reproduce the issue, go to search in the sandbox preview and press "OK" to reproduce the bug. I ran a lazy query and then quickly performed clearStore() in /src/components/Search.js to simulate it.

CodeSandbox

Thanks for looking into it!

phryneas commented 1 month ago

@DerekWang98 I can reproduce your situation, but it's a different bug.

Your codepath comes from complete while the original issue comes from notifySubscription. I'll move this over to a new issue :)

github-actions[bot] commented 1 month ago

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.

phryneas commented 1 month ago

While we can't 100% reproduce it, I believe this will be solved once we release #11989 in the next patch release of Apollo Client.

If that isn't the case, let's open a new issue on this :)

github-actions[bot] commented 2 weeks ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. For general questions, we recommend using StackOverflow or our discord server.