From 67bd8ad698bd0afe6ff7183d75da8bca4085598e Mon Sep 17 00:00:00 2001 From: Tomi Turtiainen <10324676+tomi@users.noreply.github.com> Date: Fri, 15 Dec 2023 17:35:22 +0200 Subject: [PATCH] fix(core): Handle multiple termination signals correctly (#8046) Prevent possible multiple termination signals initiating the shutdown process multiple times. --- packages/cli/src/commands/BaseCommand.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/commands/BaseCommand.ts b/packages/cli/src/commands/BaseCommand.ts index b2df412039..2f07eb6bd3 100644 --- a/packages/cli/src/commands/BaseCommand.ts +++ b/packages/cli/src/commands/BaseCommand.ts @@ -38,12 +38,14 @@ export abstract class BaseCommand extends Command { protected server?: AbstractServer; + protected isShuttingDown = false; + async init(): Promise { 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); @@ -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(); + }; + } }