From 5cbebe4547a6fee771bd6f9f2a24a97d3918946b Mon Sep 17 00:00:00 2001 From: Ashen <310210685+ashen0x@users.noreply.github.com> Date: Wed, 19 Aug 2026 03:00:15 +0530 Subject: [PATCH] fix(daemon): kill the spawned daemon when startup is abandoned (#78) --- src/start-daemon.ts | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/start-daemon.ts b/src/start-daemon.ts index e9f0672..a59d756 100644 --- a/src/start-daemon.ts +++ b/src/start-daemon.ts @@ -170,6 +170,22 @@ async function startDaemonUnlocked( proc.unref(); + // The daemon must outlive a successful start, but a start we give up on must not + // leave a detached process holding the wallet locks. `detached` makes it a group leader. + const killSpawnedDaemon = (): void => { + try { + process.kill(-proc.pid, "SIGTERM"); + } catch { + // Already exited. + } + }; + const abandonOnSignal = (): void => { + killSpawnedDaemon(); + process.exit(1); + }; + process.once("SIGINT", abandonOnSignal); + process.once("SIGTERM", abandonOnSignal); + let exitCode: number | null = null; proc.exited.then((code) => { exitCode = code; @@ -212,6 +228,8 @@ async function startDaemonUnlocked( } if (await healthyDaemonHost(port, host)) { + process.off("SIGINT", abandonOnSignal); + process.off("SIGTERM", abandonOnSignal); printStartupProgress(progressLogOffset); console.log( `Routstr daemon started (PID: ${proc.pid}, ${formatElapsed(Date.now() - startedAt)}).`, @@ -220,11 +238,10 @@ async function startDaemonUnlocked( } } + killSpawnedDaemon(); throw new Error( - `Daemon did not become healthy within ${Math.round(startupTimeoutMs / 1000)} seconds. ` + - `Process ${proc.pid} had not exited when the timeout elapsed and may still finish starting; ` + - "check 'routstrd status'. " + - `If it remains stuck, run 'kill ${proc.pid}' and try again. Check logs in ${LOGS_DIR}`, + `Daemon did not become healthy within ${Math.round(startupTimeoutMs / 1000)} seconds; ` + + `the incomplete daemon (PID ${proc.pid}) was stopped. Check logs in ${LOGS_DIR}`, ); }