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.
All that ApolloGateway.stop() does is clear a pollingTimer if it's currently set.
But what if
pollServicesis currently running (but awaiting some async operation inside updateComposition) when this is happening? Then there are two problems:pollServicesmay just end up callingpollServicesagain and schedule another call to itself, even though we thought we stopped itstopreturns while stuff is still happening, which is inappropriateI think the right fix is to have an explicit stopped flag, and also have a "pollServices is running now" promise. Something like
With a functional ApolloGateway.stop() invoked from ApolloServer.stop(), I think we can skip the unref too.