From d14bf02aed215eeecdba258bb48a330da3fc2c36 Mon Sep 17 00:00:00 2001 From: Peter Somogyvari Date: Wed, 26 Jun 2024 20:43:58 -0700 Subject: [PATCH] fix(cmd-api-server): shutdown hook was not waiting for promises 1. The library we use for having async functions handle resource deallocation was not being used correctly by the API server. 2. The library only supports callback style asynchronous code and the API server assumed that it could return a promise from the shutdown hooks which lead to the problem that our shutdown hooks were not being awaited for. Signed-off-by: Peter Somogyvari --- .../src/main/typescript/api-server.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/cactus-cmd-api-server/src/main/typescript/api-server.ts b/packages/cactus-cmd-api-server/src/main/typescript/api-server.ts index 62d00aa7ba..c49ad51ba9 100644 --- a/packages/cactus-cmd-api-server/src/main/typescript/api-server.ts +++ b/packages/cactus-cmd-api-server/src/main/typescript/api-server.ts @@ -5,7 +5,7 @@ import type { Server as SecureServer } from "https"; import type { Request, Response, RequestHandler } from "express"; import type { ServerOptions as SocketIoServerOptions } from "socket.io"; import type { Socket as SocketIoSocket } from "socket.io"; -import exitHook from "async-exit-hook"; +import exitHook, { IAsyncExitHookDoneCallback } from "async-exit-hook"; import os from "os"; import path from "path"; import tls from "tls"; @@ -208,6 +208,23 @@ export class ApiServer { level: options.config.logLevel, }); + if (this.enableShutdownHook) { + exitHook((onHookDone: IAsyncExitHookDoneCallback) => { + this.log.info("Starting async-exit-hook for cmd-api-server ..."); + this.shutdown() + .catch((ex: unknown) => { + this.log.warn("Failed async-exit-hook for cmd-api-server", ex); + throw ex; + }) + .finally(() => { + this.log.info("Concluded async-exit-hook for cmd-api-server ..."); + onHookDone(); + }); + this.log.info("Started async-exit-hook for cmd-api-server OK"); + }); + this.log.info("Registered async-exit-hook for cmd-api-server shutdown."); + } + const defaultPluginsPath = path.join( os.tmpdir(), "org",