mirror of
https://github.com/getpaseo/paseo.git
synced 2026-11-04 23:44:41 +00:00
Start the daemon after a reboot hands its old PID to another process (#5277)
Closes #5257
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
import { spawn, type ChildProcess } from "node:child_process";
|
||||
import { existsSync } from "node:fs";
|
||||
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir, uptime } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { afterEach, beforeEach, describe, expect, test } from "vitest";
|
||||
|
||||
import { readDaemonInstance, stopDaemonInstance } from "./daemon-instance.js";
|
||||
import { acquirePidLock, getPidLockInfo, isLocked, type PidLockInfo } from "./pid-lock.js";
|
||||
|
||||
// A process cannot have started before the machine booted, so a lock stamped
|
||||
// before this boot names a PID that some unrelated process now holds.
|
||||
function bootedAt(): number {
|
||||
return Date.now() - uptime() * 1000;
|
||||
}
|
||||
|
||||
async function writeLock(paseoHome: string, lock: PidLockInfo): Promise<void> {
|
||||
await writeFile(join(paseoHome, "paseo.pid"), JSON.stringify(lock));
|
||||
}
|
||||
|
||||
function lockFor(pid: number, startedAt: Date): PidLockInfo {
|
||||
return {
|
||||
pid,
|
||||
startedAt: startedAt.toISOString(),
|
||||
hostname: "old-host",
|
||||
uid: process.getuid?.() ?? 0,
|
||||
listen: "127.0.0.1:6767",
|
||||
desktopManaged: true,
|
||||
heartbeat: true,
|
||||
};
|
||||
}
|
||||
|
||||
describe("daemon instance identity across a reboot", () => {
|
||||
let paseoHome: string;
|
||||
let bystander: ChildProcess | undefined;
|
||||
|
||||
beforeEach(async () => {
|
||||
paseoHome = await mkdtemp(join(tmpdir(), "paseo-daemon-instance-"));
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
bystander?.kill("SIGKILL");
|
||||
bystander = undefined;
|
||||
await rm(paseoHome, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
test("a lock stamped before this boot has no running owner", async () => {
|
||||
await writeLock(paseoHome, lockFor(process.pid, new Date(bootedAt() - 60 * 60_000)));
|
||||
|
||||
expect(await readDaemonInstance(paseoHome)).toBeNull();
|
||||
expect(await isLocked(paseoHome)).toMatchObject({ locked: false });
|
||||
});
|
||||
|
||||
test("a supervisor started during this boot still holds the lock", async () => {
|
||||
await writeLock(paseoHome, lockFor(process.pid, new Date()));
|
||||
|
||||
expect(await readDaemonInstance(paseoHome)).toMatchObject({ pid: process.pid });
|
||||
expect(await isLocked(paseoHome)).toMatchObject({ locked: true });
|
||||
});
|
||||
|
||||
test("a new supervisor takes over a lock stamped before this boot", async () => {
|
||||
await writeLock(paseoHome, lockFor(process.pid, new Date(bootedAt() - 60 * 60_000)));
|
||||
|
||||
await acquirePidLock(paseoHome, null, { ownerPid: process.pid + 10_000 });
|
||||
|
||||
expect(await getPidLockInfo(paseoHome)).toMatchObject({ pid: process.pid + 10_000 });
|
||||
});
|
||||
|
||||
test("stopping a lock stamped before this boot leaves the process holding that pid alone", async () => {
|
||||
// Records delivery rather than dying of it, so a signal cannot be missed by arriving late.
|
||||
const signalMarker = join(paseoHome, "bystander-signalled");
|
||||
bystander = spawn(
|
||||
process.execPath,
|
||||
[
|
||||
"-e",
|
||||
`process.on("SIGTERM", () => require("node:fs").writeFileSync(${JSON.stringify(signalMarker)}, "SIGTERM"));` +
|
||||
`setTimeout(() => {}, 120_000);`,
|
||||
],
|
||||
{ stdio: "ignore" },
|
||||
);
|
||||
const bystanderPid = bystander.pid;
|
||||
if (bystanderPid === undefined) throw new Error("bystander process did not start");
|
||||
let exited = false;
|
||||
bystander.once("exit", () => {
|
||||
exited = true;
|
||||
});
|
||||
|
||||
await writeLock(paseoHome, lockFor(bystanderPid, new Date(bootedAt() - 60 * 60_000)));
|
||||
|
||||
expect(await stopDaemonInstance(paseoHome)).toMatchObject({ action: "not_running" });
|
||||
|
||||
expect(existsSync(signalMarker)).toBe(false);
|
||||
expect(exited).toBe(false);
|
||||
await expect(readFile(join(paseoHome, "paseo.pid"), "utf-8")).rejects.toThrow(/ENOENT/);
|
||||
});
|
||||
});
|
||||
@@ -4,7 +4,7 @@ import path from "node:path";
|
||||
import { setTimeout as delay } from "node:timers/promises";
|
||||
import {
|
||||
getPidLockInfo,
|
||||
isPidRunning,
|
||||
isPidLockOwnerRunning,
|
||||
isSamePidLock,
|
||||
releasePidLock,
|
||||
type PidLockInfo,
|
||||
@@ -44,7 +44,7 @@ export class DaemonInstanceError extends Error {
|
||||
|
||||
export async function readDaemonInstance(home: string): Promise<PidLockInfo | null> {
|
||||
const lock = await getPidLockInfo(home);
|
||||
return lock && isPidRunning(lock.pid) ? lock : null;
|
||||
return lock && isPidLockOwnerRunning(lock) ? lock : null;
|
||||
}
|
||||
|
||||
export function daemonLogPath(home: string): string {
|
||||
@@ -160,7 +160,7 @@ export async function stopDaemonInstance(
|
||||
`Supervisor changed for ${home}; refusing to stop PID ${instance.pid}.`,
|
||||
);
|
||||
}
|
||||
if (!instance || !isPidRunning(instance.pid)) {
|
||||
if (!instance || !isPidLockOwnerRunning(instance)) {
|
||||
if (instance)
|
||||
await releasePidLock(home, { ownerPid: instance.pid, startedAt: instance.startedAt });
|
||||
return {
|
||||
@@ -175,7 +175,7 @@ export async function stopDaemonInstance(
|
||||
let { forced, usedLifecycleRpc } = await requestInstanceStop(home, instance, options);
|
||||
const waitForExit = async (waitMs: number) => {
|
||||
const exitDeadline = Date.now() + waitMs;
|
||||
while (isPidRunning(instance.pid)) {
|
||||
while (isPidLockOwnerRunning(instance)) {
|
||||
const current = await getPidLockInfo(home);
|
||||
if (current && !isSamePidLock(instance, current))
|
||||
throw new DaemonInstanceError(
|
||||
|
||||
@@ -73,7 +73,7 @@ describe("pid-lock ownership", () => {
|
||||
pidPath,
|
||||
JSON.stringify({
|
||||
pid: process.pid,
|
||||
startedAt: "2026-01-01T00:00:00.000Z",
|
||||
startedAt: new Date().toISOString(),
|
||||
hostname: "old-host",
|
||||
uid: process.getuid?.() ?? 0,
|
||||
listen: "127.0.0.1:6767",
|
||||
@@ -106,7 +106,7 @@ describe("pid-lock ownership", () => {
|
||||
pidPath,
|
||||
JSON.stringify({
|
||||
pid: process.pid,
|
||||
startedAt: "2026-01-01T00:00:00.000Z",
|
||||
startedAt: new Date().toISOString(),
|
||||
hostname: "old-host",
|
||||
uid: process.getuid?.() ?? 0,
|
||||
listen: "127.0.0.1:6767",
|
||||
@@ -138,7 +138,7 @@ describe("pid-lock ownership", () => {
|
||||
pidPath,
|
||||
JSON.stringify({
|
||||
pid: process.pid,
|
||||
startedAt: "2026-01-01T00:00:00.000Z",
|
||||
startedAt: new Date().toISOString(),
|
||||
hostname: "old-host",
|
||||
uid: process.getuid?.() ?? 0,
|
||||
listen: "127.0.0.1:6767",
|
||||
@@ -169,7 +169,7 @@ describe("pid-lock ownership", () => {
|
||||
pidPath,
|
||||
JSON.stringify({
|
||||
pid: process.pid,
|
||||
startedAt: "2026-01-01T00:00:00.000Z",
|
||||
startedAt: new Date().toISOString(),
|
||||
hostname: "old-host",
|
||||
uid: process.getuid?.() ?? 0,
|
||||
listen: "127.0.0.1:6767",
|
||||
|
||||
@@ -2,7 +2,7 @@ import { open, readFile, unlink, utimes } from "node:fs/promises";
|
||||
import type { FileHandle } from "node:fs/promises";
|
||||
import { ensurePrivateDirectory } from "./private-files.js";
|
||||
import { join } from "node:path";
|
||||
import { hostname } from "node:os";
|
||||
import { hostname, uptime } from "node:os";
|
||||
import { z } from "zod";
|
||||
|
||||
export const pidLockInfoSchema = z.object({
|
||||
@@ -40,7 +40,7 @@ const PID_LOCK_HEARTBEAT_INTERVAL_MS = 30_000;
|
||||
const PID_LOCK_READ_RETRY_ATTEMPTS = 10;
|
||||
const PID_LOCK_READ_RETRY_DELAY_MS = 50;
|
||||
|
||||
export function isPidRunning(pid: number): boolean {
|
||||
function isPidRunning(pid: number): boolean {
|
||||
try {
|
||||
process.kill(pid, 0);
|
||||
return true;
|
||||
@@ -49,6 +49,30 @@ export function isPidRunning(pid: number): boolean {
|
||||
}
|
||||
}
|
||||
|
||||
// `uptime()` reports whole seconds on some platforms, so the derived boot instant carries
|
||||
// about a second of error. This covers that resolution and nothing more: every extra second
|
||||
// is a window in which a lock written just before a reboot still reads as current.
|
||||
const BOOT_INSTANT_TOLERANCE_MS = 5_000;
|
||||
|
||||
function precedesThisBoot(startedAt: string): boolean {
|
||||
const stamped = Date.parse(startedAt);
|
||||
if (Number.isNaN(stamped)) return false;
|
||||
return stamped < Date.now() - uptime() * 1000 - BOOT_INSTANT_TOLERANCE_MS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the process that wrote this lock is still running.
|
||||
*
|
||||
* A PID alone does not identify the supervisor: the operating system hands the number to
|
||||
* something else once the supervisor is gone, and a reboot reassigns it freely. A process
|
||||
* cannot predate the boot it runs under, so a lock stamped before this boot is abandoned
|
||||
* however alive its PID looks.
|
||||
*/
|
||||
export function isPidLockOwnerRunning(lock: PidLockInfo): boolean {
|
||||
if (precedesThisBoot(lock.startedAt)) return false;
|
||||
return isPidRunning(lock.pid);
|
||||
}
|
||||
|
||||
function getPidFilePath(paseoHome: string): string {
|
||||
return join(paseoHome, "paseo.pid");
|
||||
}
|
||||
@@ -105,7 +129,7 @@ async function clearExistingPidLock(
|
||||
existingLock: PidLockInfo,
|
||||
lockOwnerPid: number,
|
||||
): Promise<"already_owned" | "cleared"> {
|
||||
const lockOwnerRunning = isPidRunning(existingLock.pid);
|
||||
const lockOwnerRunning = isPidLockOwnerRunning(existingLock);
|
||||
if (existingLock.pid === lockOwnerPid && lockOwnerRunning) {
|
||||
await touchPidLockFile(pidPath);
|
||||
return "already_owned";
|
||||
@@ -116,7 +140,7 @@ async function clearExistingPidLock(
|
||||
if (
|
||||
!confirmedLock ||
|
||||
!isSamePidLock(existingLock, confirmedLock) ||
|
||||
isPidRunning(confirmedLock.pid)
|
||||
isPidLockOwnerRunning(confirmedLock)
|
||||
) {
|
||||
throw new PidLockError("PID lock changed while checking whether it was abandoned");
|
||||
}
|
||||
@@ -339,7 +363,7 @@ export async function isLocked(
|
||||
if (!info) {
|
||||
return { locked: false };
|
||||
}
|
||||
if (!isPidRunning(info.pid)) {
|
||||
if (!isPidLockOwnerRunning(info)) {
|
||||
return { locked: false, info };
|
||||
}
|
||||
return { locked: true, info };
|
||||
|
||||
Reference in New Issue
Block a user