Skip to content

Commit

Permalink
fix(core): Handle multiple termination signals correctly (#8046)
Browse files Browse the repository at this point in the history
Prevent possible multiple termination signals initiating the shutdown
process multiple times.
  • Loading branch information
tomi authored Dec 15, 2023
1 parent e69707e commit 67bd8ad
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions packages/cli/src/commands/BaseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ export abstract class BaseCommand extends Command {

protected server?: AbstractServer;

protected isShuttingDown = false;

async init(): Promise<void> {
await initErrorHandling();
initExpressionEvaluator();

process.once('SIGTERM', async () => this.stopProcess());
process.once('SIGINT', async () => this.stopProcess());
process.once('SIGTERM', this.onTerminationSignal('SIGTERM'));
process.once('SIGINT', this.onTerminationSignal('SIGINT'));

// Make sure the settings exist
this.instanceSettings = Container.get(InstanceSettings);
Expand Down Expand Up @@ -299,4 +301,17 @@ export abstract class BaseCommand extends Command {
const exitCode = error instanceof ExitError ? error.oclif.exit : error ? 1 : 0;
this.exit(exitCode);
}

private onTerminationSignal(signal: string) {
return async () => {
if (this.isShuttingDown) {
this.logger.info(`Received ${signal}. Already shutting down...`);
return;
}

this.logger.info(`Received ${signal}. Shutting down...`);
this.isShuttingDown = true;
await this.stopProcess();
};
}
}

0 comments on commit 67bd8ad

Please sign in to comment.