Skip to content

Commit

Permalink
fix(cmd-api-server): shutdown hook was not waiting for promises
Browse files Browse the repository at this point in the history
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 <peter.somogyvari@accenture.com>
  • Loading branch information
petermetz committed Jul 2, 2024
1 parent eaba573 commit d14bf02
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion packages/cactus-cmd-api-server/src/main/typescript/api-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit d14bf02

Please sign in to comment.