question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Store reset while query was in flight(not completed in link chain)" when stopQuery is called right after resetStore. Race condition?

See original GitHub issue

Currently, queries are getting stuck in the QueryManager.fetchQueryPromises Map when stopQuery is called while that query is in flight.

Inside QueryManager.fetchQuery a subscription is added to the QueryInfo (QueryManager.query object) of the Observable representing the current request.

      this.setQuery(queryId, ({ subscriptions }) => ({
        subscriptions: subscriptions.concat([subscription]),
      }));

On the subscription, callbacks are set up to remove the queryId from QueryManager.fetchQueryPromises

        error: (error: ApolloError) => {
          this.removeFetchQueryPromise(requestId);
          // ...
        },
        complete: () => {
          this.removeFetchQueryPromise(requestId);
          // ...
        },

When stopQuery() is called before this query resolves, the promise will stay in that map because the subscription has been removed, therefore the callbacks to remove it will no long trigger.

  public stopQuery(queryId: string) {
    this.stopQueryInStore(queryId);
    this.removeQuery(queryId);
  }

  public removeQuery(queryId: string) {
    const { subscriptions } = this.getQuery(queryId);
    // teardown all links
    subscriptions.forEach(x => x.unsubscribe());
    this.queries.delete(queryId);
  }

Later on, this will cause problems when one tries to call resetStore()

  public resetStore(): Promise<ApolloQueryResult<any>[]> {
    return this.clearStore().then(() => {
      return this.reFetchObservableQueries();
    });
  }

  public clearStore(): Promise<void> {
    this.fetchQueryPromises.forEach(({ reject }) => {
      reject(
        new Error(
          'Store reset while query was in flight(not completed in link chain)',
        ),
      );
    });
    // ...
  }
Error: Network error: Store reset while query was in flight(not completed in link chain)
    at new ApolloError (bundle.umd.js:124)

Intended outcome: I think queries should be removed from that Map after subscriptions get removed.

So as a solution, I might suggest that as part of the clean up for stopQuery, that the requestId should be removed from QueryManager.fetchQueryPromises. Perhaps there is a cancel method to evoke, or I should reject the promise with a new Error that the query has been cancelled.

Actual outcome: Queries are staying in that Map.

How to reproduce the issue: It’s an edge case, but this is how I ran into this issue through react-apollo:

  • Render a Query wrapped component.
  • Call resetStore() via user interaction
  • Immediately trigger a state change that results in that Query component being unmounted.

Versions

 apollo-client: 2.3.5
 react-apollo: 2.1.9

In hindsight I should have just written a PR instead.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:18 (7 by maintainers)

github_iconTop GitHub Comments

52reactions
qeternitycommented, Sep 18, 2019

Can confirm this is still an issue

46reactions
JanSmolkocommented, Jan 24, 2020

If somebody has still this issue… I resolved it with stop() before clearStore()

...
client.stop()
client.clearStore()
...
Read more comments on GitHub >

github_iconTop Results From Across the Web

Store reset while query was in flight (not completed in link chain)
when I log out my user. the code is const onSignout = async () => { removeToken(); client.cache.reset(); await client.resetStore(); ...
Read more >
"Store reset while query was in flight(not completed in link ...
"Store reset while query was in flight(not completed in link chain)" when stopQuery is called right after resetStore. Race condition?
Read more >
clearStore() vs resetStore() - Help - Apollo GraphQL
resetStore () after your login or logout process has completed. This will cause the store to be cleared and all active queries to...
Read more >
Transaction Details - Galileo POA Explorer
cancelPendingFetches(new InvariantError(\n 'Store reset while query was in flight (not completed in link chain)',\n ));\n\n this.queries.
Read more >
COMPIT'13 - Scinapse
After an accident, damaged ships or offshore structures often must be towed to ... A creative approach, not just as a “clear box”...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found