Skip to content

ApolloGateway.stop() is not reliable #4428

@glasser

Description

@glasser

All that ApolloGateway.stop() does is clear a pollingTimer if it's currently set.

But what if pollServices is currently running (but awaiting some async operation inside updateComposition) when this is happening? Then there are two problems:

  • pollServices may just end up calling pollServices again and schedule another call to itself, even though we thought we stopped it
  • stop returns while stuff is still happening, which is inappropriate

I think the right fix is to have an explicit stopped flag, and also have a "pollServices is running now" promise. Something like

function pollServices() {
  // assert this.pollServicesIsRunning is null
  this.pollServicesIsRunning = actualPollServices()
    .catch(e => console.log("error in actualPollServices"));
    .then(() => {
      this.pollServicesIsRunning = null;
      if (!this.stopped) this.pollTimer = setTimeout(() => this.pollServices(), TIMEOUT);
    });
}

async function actualPollServices() {
  // ... actually poll services; no need to sleep or reschedule
}

async function stop() {
  this.stopped = true;
  if (this.pollTimer) cancelTimeout(this.pollTimer);
  if (this.pollServicesIsRunning) await this.pollServicesIsRunning;
}

With a functional ApolloGateway.stop() invoked from ApolloServer.stop(), I think we can skip the unref too.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions