mirror of
https://github.com/Routstr/routstrd.git
synced 2026-10-05 20:38:22 +00:00
fix(daemon): kill the spawned daemon when startup is abandoned (#78)
This commit is contained in:
+21
-4
@@ -170,6 +170,22 @@ async function startDaemonUnlocked(
|
|||||||
|
|
||||||
proc.unref();
|
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;
|
let exitCode: number | null = null;
|
||||||
proc.exited.then((code) => {
|
proc.exited.then((code) => {
|
||||||
exitCode = code;
|
exitCode = code;
|
||||||
@@ -212,6 +228,8 @@ async function startDaemonUnlocked(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (await healthyDaemonHost(port, host)) {
|
if (await healthyDaemonHost(port, host)) {
|
||||||
|
process.off("SIGINT", abandonOnSignal);
|
||||||
|
process.off("SIGTERM", abandonOnSignal);
|
||||||
printStartupProgress(progressLogOffset);
|
printStartupProgress(progressLogOffset);
|
||||||
console.log(
|
console.log(
|
||||||
`Routstr daemon started (PID: ${proc.pid}, ${formatElapsed(Date.now() - startedAt)}).`,
|
`Routstr daemon started (PID: ${proc.pid}, ${formatElapsed(Date.now() - startedAt)}).`,
|
||||||
@@ -220,11 +238,10 @@ async function startDaemonUnlocked(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
killSpawnedDaemon();
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Daemon did not become healthy within ${Math.round(startupTimeoutMs / 1000)} seconds. ` +
|
`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; ` +
|
`the incomplete daemon (PID ${proc.pid}) was stopped. Check logs in ${LOGS_DIR}`,
|
||||||
"check 'routstrd status'. " +
|
|
||||||
`If it remains stuck, run 'kill ${proc.pid}' and try again. Check logs in ${LOGS_DIR}`,
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user