From 32f03eaaa2f569917b01a5e67c138900a748ed84 Mon Sep 17 00:00:00 2001 From: MathieuG-P <40181755+Zagrios@users.noreply.github.com> Date: Mon, 20 May 2024 21:43:11 +0200 Subject: [PATCH] [bugfix] Fix the issue where the launch shortcut window never closes after launching BS --- .../bs-launcher/abstract-launcher.service.ts | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/services/bs-launcher/abstract-launcher.service.ts b/src/main/services/bs-launcher/abstract-launcher.service.ts index b5827999..7dfb4946 100644 --- a/src/main/services/bs-launcher/abstract-launcher.service.ts +++ b/src/main/services/bs-launcher/abstract-launcher.service.ts @@ -3,6 +3,7 @@ import { BSLocalVersionService } from "../bs-local-version.service"; import { ChildProcessWithoutNullStreams, SpawnOptionsWithoutStdio, spawn } from "child_process"; import path from "path"; import log from "electron-log"; +import { sToMs } from "shared/helpers/time.helpers"; export abstract class AbstractLauncherService { @@ -46,20 +47,40 @@ export abstract class AbstractLauncherService { } - protected launchBs(bsExePath: string, args: string[], options?: SpawnOptionsWithoutStdio): {process: ChildProcessWithoutNullStreams, exit: Promise} { + protected launchBs(bsExePath: string, args: string[], options?: SpawnBsProcessOptions): {process: ChildProcessWithoutNullStreams, exit: Promise} { const process = this.launchBSProcess(bsExePath, args, options); + let timoutId: NodeJS.Timeout; + const exit = new Promise((resolve, reject) => { + process.on("error", (err) => { log.error(`Error while launching BS`, err); reject(err); }); + process.on("exit", (code) => { log.info(`BS process exit with code ${code}`); resolve(code); }); + + const unrefAfter = options?.unrefAfter ?? sToMs(10); + + timoutId = setTimeout(() => { + log.error("BS process unref after timeout", unrefAfter); + process.unref(); + process.removeAllListeners(); + resolve(-1); + }, unrefAfter); + + }).finally(() => { + clearTimeout(timoutId); }); return { process, exit }; } } + +export type SpawnBsProcessOptions = { + unrefAfter?: number; +} & SpawnOptionsWithoutStdio;