From f3ccad192c7b73c60f155e9796cbde97e8202d07 Mon Sep 17 00:00:00 2001 From: J H Date: Wed, 6 Mar 2024 15:43:07 -0700 Subject: [PATCH] chore: Add the process tree destroyer --- .../src/Adapters/HostSystemStartOs.ts | 12 +++++- sdk/lib/mainFn/Daemons.ts | 2 + sdk/lib/util/utils.ts | 41 ++++++++++++++++--- 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/container-runtime/src/Adapters/HostSystemStartOs.ts b/container-runtime/src/Adapters/HostSystemStartOs.ts index 007f783c0..ec4fac796 100644 --- a/container-runtime/src/Adapters/HostSystemStartOs.ts +++ b/container-runtime/src/Adapters/HostSystemStartOs.ts @@ -110,11 +110,21 @@ export class HostSystemStartOs implements Effects { T.Effects["clearServiceInterfaces"] > } - createOverlayedImage(options: { imageId: string }): Promise { + createOverlayedImage(options: { + imageId: string + }): Promise<[string, string]> { return this.rpcRound("createOverlayedImage", options) as ReturnType< T.Effects["createOverlayedImage"] > } + destroyOverlayedImage(options: { + imageId: string + guid: string + }): Promise { + return this.rpcRound("destroyOverlayedImage", options) as ReturnType< + T.Effects["destroyOverlayedImage"] + > + } executeAction(...[options]: Parameters) { return this.rpcRound("executeAction", options) as ReturnType< T.Effects["executeAction"] diff --git a/sdk/lib/mainFn/Daemons.ts b/sdk/lib/mainFn/Daemons.ts index 45ff723d0..e9986ad3f 100644 --- a/sdk/lib/mainFn/Daemons.ts +++ b/sdk/lib/mainFn/Daemons.ts @@ -137,6 +137,7 @@ export class Daemons { } return { async term(options?: { signal?: Signals; timeout?: number }) { + console.error("Bluj Daemons term") await Promise.all( Object.values>(daemonsStarted).map((x) => x.then((x) => x.term(options)), @@ -144,6 +145,7 @@ export class Daemons { ) }, async wait() { + console.error("Bluj Daemons wait") await Promise.all( Object.values>(daemonsStarted).map((x) => x.then((x) => x.wait()), diff --git a/sdk/lib/util/utils.ts b/sdk/lib/util/utils.ts index cb5afae3d..e8eb7f6e0 100644 --- a/sdk/lib/util/utils.ts +++ b/sdk/lib/util/utils.ts @@ -254,11 +254,21 @@ export const createUtils = < }) }) + const pid = childProcess.pid return { - wait() { - return answer + async wait() { + const pids = pid ? await psTree(pid, overlay) : [] + try { + return await answer + } finally { + for (const process of pids) { + overlay.exec(["kill", `-9`, String(process)]) + } + } }, async term({ signal = SIGTERM, timeout = NO_TIMEOUT } = {}) { + const pids = pid ? await psTree(pid, overlay) : [] + console.error("Bluj killing pid ", pids) try { childProcess.kill(signal) @@ -269,13 +279,26 @@ export const createUtils = < ), answer.then(() => false), ]) - if (didTimeout) childProcess.kill(SIGKILL) - return + if (didTimeout) { + childProcess.kill(SIGKILL) + } + } else { + await answer } - await answer } finally { await overlay.destroy() } + + console.error("Bluj actually killing pid ", pids) + try { + for (const process of pids) { + await overlay.exec(["kill", `-${signal}`, String(process)]) + } + } finally { + for (const process of pids) { + overlay.exec(["kill", `-9`, String(process)]) + } + } }, } }, @@ -294,3 +317,11 @@ export const createUtils = < } } function noop(): void {} + +async function psTree(pid: number, overlay: Overlay): Promise { + const { stdout } = await overlay.exec(["pstree", `-p`, String(pid)]) + const regex: RegExp = /\((\d+)\)/g + return [...stdout.toString().matchAll(regex)].map(([_all, pid]) => + parseInt(pid), + ) +}