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.

Having multiple polling queries on the same QueryManager causes setInterval leak

See original GitHub issue

I discovered this one via a React component (with Apollo connect container) that had multiple polling queries. Each time I unmounted & mounted the component, I would get a polling leak, as evidenced by increasingly frequent HTTP requests to my /graphql endpoint.

I put some console logging in this zone https://github.com/apollostack/apollo-client/blob/master/src/QueryManager.ts#L426-L455, like this:

  private startQuery(options: WatchQueryOptions, listener: QueryListener) {
    const queryId = this.generateQueryId();
    this.queryListeners[queryId] = listener;
    this.fetchQuery(queryId, options);
    if (options.pollInterval) {
      this.pollingTimer = setInterval(() => {
        const pollingOptions = assign({}, options) as WatchQueryOptions;
        // subsequent fetches from polling always reqeust new data
        pollingOptions.forceFetch = true;
        this.fetchQuery(queryId, pollingOptions);
      }, options.pollInterval);
+++   console.log('starting timer', this.pollingTimer)
    }
    return queryId;
  }

  private stopQuery(queryId: string) {
    // XXX in the future if we should cancel the request
    // so that it never tries to return data
    delete this.queryListeners[queryId];

    // if we have a polling interval running, stop it
    if (this.pollingTimer) {
+++   console.log('stopping timer', this.pollingTimer)
      clearInterval(this.pollingTimer);
    }

    this.store.dispatch({
      type: 'APOLLO_QUERY_STOP',
      queryId,
    });
  }

I mounted & unmounted the component twice, and the component sets up 3 polling queries on every mount. Here’s the result:

screen shot 2016-05-27 at 3 02 53 pm

As you can see it looks like we have lost (overwritten?) our references to timers 94, 128, 146, & 147.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:13 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
Poincarecommented, Jun 1, 2016

I think that issue is currently mentioned in the code as something to fix. It seems the polling structure requires some pretty significant changes so that (1) polling doesn’t leak (2) we can keep track of the queries that have been sent to the server (probably by query id) and don’t re-fire the same query again.

I think fixing this issue is actually pretty intimately connected to the batching over HTTP since I’ve been building a tick-based scheduler-like system for that anyway. Hopefully, that will make it easier to resolve this issue and others that involve the current polling system.

1reaction
Poincarecommented, Jun 1, 2016

Sure, I’ll look into this in detail tomorrow and hopefully make some headway on some kind of fix.

Read more comments on GitHub >

github_iconTop Results From Across the Web

setInterval causes memory-leak - javascript - Stack Overflow
The problem here is that every time we fetch, we increase our memory with the size of the image, and it never seems...
Read more >
@apollo/client | Yarn - Package Manager
Apollo Client is a fully-featured caching GraphQL client with ... Better handle deferred queries that have cached or partial cached data for them....
Read more >
Polling and memory usage - Homey Community Forum
I use node-fetch for json requests, and I've tried a few approaches to pooling: the same as in the shelly app - SetInterval...
Read more >
https://raw.githubusercontent.com/bendenoz/apollo-...
directive to keep `feed` data from different queries separate within the ... with multiple ways to reenable it (per-cache, per-query, or a mixture...
Read more >
Why setInterval() is Bad - x5ff
Probably every programmer likes efficiency, but efficient memory management is even more important if you don't have a lot. Consequently I ...
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