fix(server): fix daemon startup race and reduce log bloat

The desktop app could time out connecting to the daemon on first launch
because the PID file advertised a listen address before the HTTP server
was actually listening. The supervisor now writes the PID lock with
listen: null and updates it only after the worker sends a paseo:ready
IPC message confirming the server is listening.

- Rename daemon-runner.ts to supervisor-entrypoint.ts
- PID lock acquired with listen: null, updated via paseo:ready IPC
- Worker sends paseo:ready after httpServer.listen() resolves
- Desktop polls until listen is non-null before returning to the app
- Remove PASEO_PID_LOCK_MODE and external lock mode
- Remove unnecessary env overrides (PASEO_HOME, PASEO_CORS_ORIGINS) from
  desktop daemon spawn
- Reduce trace log bloat: inbound/outbound WebSocket messages now log
  only message type and payload size instead of full payloads
- Supervisor restarts worker on SIGKILL (covers OOM)
This commit is contained in:
Mohamed Boudra
2026-03-30 12:37:07 +07:00
parent e758734807
commit 8d0a9f9224
21 changed files with 198 additions and 147 deletions
+1 -1
View File
@@ -114,7 +114,7 @@ buildNpmPackage rec {
fi fi
done done
# Copy server scripts (daemon-runner, supervisor) needed by CLI # Copy server scripts (including supervisor-entrypoint) needed by CLI
if [ -d packages/server/dist/scripts ]; then if [ -d packages/server/dist/scripts ]; then
mkdir -p $out/lib/paseo/packages/server/dist/scripts mkdir -p $out/lib/paseo/packages/server/dist/scripts
cp -a packages/server/dist/scripts/* $out/lib/paseo/packages/server/dist/scripts/ cp -a packages/server/dist/scripts/* $out/lib/paseo/packages/server/dist/scripts/
@@ -6,7 +6,7 @@ export type DesktopDaemonState = "starting" | "running" | "stopped" | "errored";
export type DesktopDaemonStatus = { export type DesktopDaemonStatus = {
serverId: string; serverId: string;
status: DesktopDaemonState; status: DesktopDaemonState;
listen: string; listen: string | null;
hostname: string | null; hostname: string | null;
pid: number | null; pid: number | null;
home: string; home: string;
@@ -82,7 +82,7 @@ function parseDesktopDaemonStatus(raw: unknown): DesktopDaemonStatus {
return { return {
serverId: toStringOrNull(raw.serverId) ?? "", serverId: toStringOrNull(raw.serverId) ?? "",
status: parseDesktopDaemonState(raw.status), status: parseDesktopDaemonState(raw.status),
listen: toStringOrNull(raw.listen) ?? "", listen: toStringOrNull(raw.listen),
hostname: toStringOrNull(raw.hostname), hostname: toStringOrNull(raw.hostname),
pid: toNumberOrNull(raw.pid), pid: toNumberOrNull(raw.pid),
home: toStringOrNull(raw.home) ?? "", home: toStringOrNull(raw.home) ?? "",
+1 -1
View File
@@ -1172,7 +1172,7 @@ export class HostRuntimeStore {
async bootstrapDesktop(): Promise<HostRuntimeBootstrapResult> { async bootstrapDesktop(): Promise<HostRuntimeBootstrapResult> {
try { try {
const daemon = await startDesktopDaemon(); const daemon = await startDesktopDaemon();
const listenAddress = daemon.listen.trim(); const listenAddress = daemon.listen?.trim() ?? "";
const serverId = daemon.serverId.trim(); const serverId = daemon.serverId.trim();
if (!listenAddress) { if (!listenAddress) {
return { return {
@@ -124,11 +124,16 @@ function resolveDaemonRunnerEntry(): string {
try { try {
const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8")) as { name?: string }; const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8")) as { name?: string };
if (packageJson.name === "@getpaseo/server") { if (packageJson.name === "@getpaseo/server") {
const distRunner = path.join(currentDir, "dist", "scripts", "daemon-runner.js"); const distRunner = path.join(
currentDir,
"dist",
"scripts",
"supervisor-entrypoint.js",
);
if (existsSync(distRunner)) { if (existsSync(distRunner)) {
return distRunner; return distRunner;
} }
return path.join(currentDir, "scripts", "daemon-runner.ts"); return path.join(currentDir, "scripts", "supervisor-entrypoint.ts");
} }
} catch { } catch {
// Continue searching up if package.json exists but is invalid. // Continue searching up if package.json exists but is invalid.
@@ -2,7 +2,7 @@
/** /**
* Regression: `paseo daemon stop` must stop supervised dev daemons * Regression: `paseo daemon stop` must stop supervised dev daemons
* without allowing daemon-runner to respawn a new worker process. * without allowing the supervisor entrypoint to respawn a new worker process.
*/ */
import assert from "node:assert"; import assert from "node:assert";
@@ -120,20 +120,24 @@ let supervisorProcess: ChildProcess | null = null;
let recentSupervisorLogs = ""; let recentSupervisorLogs = "";
try { try {
console.log("Test 1: start daemon-runner in dev mode with isolated PASEO_HOME"); console.log("Test 1: start supervisor-entrypoint in dev mode with isolated PASEO_HOME");
supervisorProcess = spawn("npx", ["tsx", "../server/scripts/daemon-runner.ts", "--dev"], { supervisorProcess = spawn(
cwd: cliRoot, "npx",
env: { ["tsx", "../server/scripts/supervisor-entrypoint.ts", "--dev"],
...process.env, {
...testEnv, cwd: cliRoot,
PASEO_HOME: paseoHome, env: {
PASEO_LISTEN: `127.0.0.1:${port}`, ...process.env,
PASEO_RELAY_ENABLED: "false", ...testEnv,
CI: "true", PASEO_HOME: paseoHome,
PASEO_LISTEN: `127.0.0.1:${port}`,
PASEO_RELAY_ENABLED: "false",
CI: "true",
},
stdio: ["ignore", "pipe", "pipe"],
}, },
stdio: ["ignore", "pipe", "pipe"], );
});
supervisorProcess.stdout?.on("data", (chunk) => { supervisorProcess.stdout?.on("data", (chunk) => {
recentSupervisorLogs = (recentSupervisorLogs + chunk.toString()).slice(-8000); recentSupervisorLogs = (recentSupervisorLogs + chunk.toString()).slice(-8000);
@@ -161,8 +165,9 @@ try {
const command = readProcessCommand(daemonPid); const command = readProcessCommand(daemonPid);
assert(command !== null, "pid lock pid should resolve to a running process command"); assert(command !== null, "pid lock pid should resolve to a running process command");
assert( assert(
command.includes("daemon-runner.ts") || command.includes("daemon-runner.js"), command.includes("supervisor-entrypoint.ts") ||
`pid lock pid should be daemon-runner process, got: ${command}`, command.includes("supervisor-entrypoint.js"),
`pid lock pid should be supervisor-entrypoint process, got: ${command}`,
); );
console.log(`✓ dev daemon started with daemon pid ${daemonPid}\n`); console.log(`✓ dev daemon started with daemon pid ${daemonPid}\n`);
@@ -186,7 +191,7 @@ try {
await waitFor( await waitFor(
() => !isProcessRunning(supervisorProcess!.pid ?? -1), () => !isProcessRunning(supervisorProcess!.pid ?? -1),
15000, 15000,
"daemon-runner supervisor remained running after stop", "supervisor-entrypoint process remained running after stop",
); );
} }
@@ -1,7 +1,7 @@
#!/usr/bin/env npx tsx #!/usr/bin/env npx tsx
/** /**
* Regression: a single SIGINT sent to a supervised daemon-runner must allow * Regression: a single SIGINT sent to the supervised supervisor entrypoint must allow
* graceful daemon lifecycle shutdown to complete (no early forced exit path). * graceful daemon lifecycle shutdown to complete (no early forced exit path).
*/ */
@@ -131,21 +131,25 @@ let supervisorProcess: ChildProcess | null = null;
let recentSupervisorLogs = ""; let recentSupervisorLogs = "";
try { try {
console.log("Test 1: start daemon-runner in dev mode with isolated PASEO_HOME"); console.log("Test 1: start supervisor-entrypoint in dev mode with isolated PASEO_HOME");
supervisorProcess = spawn("npx", ["tsx", "../server/scripts/daemon-runner.ts", "--dev"], { supervisorProcess = spawn(
cwd: cliRoot, "npx",
env: { ["tsx", "../server/scripts/supervisor-entrypoint.ts", "--dev"],
...process.env, {
...testEnv, cwd: cliRoot,
PASEO_HOME: paseoHome, env: {
PASEO_LISTEN: `127.0.0.1:${port}`, ...process.env,
PASEO_RELAY_ENABLED: "false", ...testEnv,
CI: "true", PASEO_HOME: paseoHome,
PASEO_LISTEN: `127.0.0.1:${port}`,
PASEO_RELAY_ENABLED: "false",
CI: "true",
},
stdio: ["ignore", "pipe", "pipe"],
detached: process.platform !== "win32",
}, },
stdio: ["ignore", "pipe", "pipe"], );
detached: process.platform !== "win32",
});
supervisorProcess.stdout?.on("data", (chunk) => { supervisorProcess.stdout?.on("data", (chunk) => {
recentSupervisorLogs = (recentSupervisorLogs + chunk.toString()).slice(-8000); recentSupervisorLogs = (recentSupervisorLogs + chunk.toString()).slice(-8000);
@@ -62,7 +62,7 @@ try {
"-e", "-e",
// Keep the process alive long enough for stop command assertions. // Keep the process alive long enough for stop command assertions.
"setInterval(() => {}, 1000)", "setInterval(() => {}, 1000)",
"daemon-runner.ts", "supervisor-entrypoint.ts",
], ],
{ {
env: { env: {
@@ -122,20 +122,24 @@ let supervisorProcess: ChildProcess | null = null;
let recentSupervisorLogs = ""; let recentSupervisorLogs = "";
try { try {
console.log("Test 1: start daemon-runner in dev mode with isolated PASEO_HOME"); console.log("Test 1: start supervisor-entrypoint in dev mode with isolated PASEO_HOME");
supervisorProcess = spawn("npx", ["tsx", "../server/scripts/daemon-runner.ts", "--dev"], { supervisorProcess = spawn(
cwd: cliRoot, "npx",
env: { ["tsx", "../server/scripts/supervisor-entrypoint.ts", "--dev"],
...process.env, {
...testEnv, cwd: cliRoot,
PASEO_HOME: paseoHome, env: {
PASEO_LISTEN: host, ...process.env,
PASEO_RELAY_ENABLED: "false", ...testEnv,
CI: "true", PASEO_HOME: paseoHome,
PASEO_LISTEN: host,
PASEO_RELAY_ENABLED: "false",
CI: "true",
},
stdio: ["ignore", "pipe", "pipe"],
}, },
stdio: ["ignore", "pipe", "pipe"], );
});
supervisorProcess.stdout?.on("data", (chunk) => { supervisorProcess.stdout?.on("data", (chunk) => {
recentSupervisorLogs = (recentSupervisorLogs + chunk.toString()).slice(-8000); recentSupervisorLogs = (recentSupervisorLogs + chunk.toString()).slice(-8000);
@@ -134,11 +134,6 @@ try {
env: { env: {
...process.env, ...process.env,
...testEnv, ...testEnv,
// This test validates direct unsupervised worker ownership semantics.
// Agent-orchestrated shells may export PASEO_PID_LOCK_MODE=external,
// which would delegate lock ownership away from this process and make
// daemon status checks fail to observe a running owner PID.
PASEO_PID_LOCK_MODE: "self",
PASEO_HOME: paseoHome, PASEO_HOME: paseoHome,
PASEO_LISTEN: host, PASEO_LISTEN: host,
PASEO_RELAY_ENABLED: "false", PASEO_RELAY_ENABLED: "false",
+13 -40
View File
@@ -3,7 +3,7 @@ import { existsSync, readFileSync } from "node:fs";
import path from "node:path"; import path from "node:path";
import { app, ipcMain } from "electron"; import { app, ipcMain } from "electron";
import log from "electron-log/main"; import log from "electron-log/main";
import { loadConfig, resolvePaseoHome, getOrCreateServerId } from "@getpaseo/server"; import { resolvePaseoHome, getOrCreateServerId } from "@getpaseo/server";
import { import {
copyAttachmentFileToManagedStorage, copyAttachmentFileToManagedStorage,
deleteManagedAttachmentFile, deleteManagedAttachmentFile,
@@ -27,14 +27,13 @@ const STARTUP_POLL_MAX_ATTEMPTS = 150;
const STOP_TIMEOUT_MS = 15_000; const STOP_TIMEOUT_MS = 15_000;
const KILL_TIMEOUT_MS = 3_000; const KILL_TIMEOUT_MS = 3_000;
const DETACHED_STARTUP_GRACE_MS = 1200; const DETACHED_STARTUP_GRACE_MS = 1200;
const DEFAULT_ELECTRON_DEV_SERVER_URL = "http://localhost:8081";
type DesktopDaemonState = "starting" | "running" | "stopped" | "errored"; type DesktopDaemonState = "starting" | "running" | "stopped" | "errored";
type DesktopDaemonStatus = { type DesktopDaemonStatus = {
serverId: string; serverId: string;
status: DesktopDaemonState; status: DesktopDaemonState;
listen: string; listen: string | null;
hostname: string | null; hostname: string | null;
pid: number | null; pid: number | null;
home: string; home: string;
@@ -148,30 +147,6 @@ function logDesktopDaemonLifecycle(message: string, details?: Record<string, unk
}); });
} }
function buildDesktopDaemonCorsOriginsEnv(): string | undefined {
const origins = new Set(
(process.env.PASEO_CORS_ORIGINS ?? "")
.split(",")
.map((value) => value.trim())
.filter((value) => value.length > 0),
);
const devServerUrl = process.env.EXPO_DEV_URL ?? DEFAULT_ELECTRON_DEV_SERVER_URL;
try {
const parsed = new URL(devServerUrl);
origins.add(parsed.origin);
if (parsed.hostname === "localhost") {
origins.add(`${parsed.protocol}//127.0.0.1${parsed.port ? `:${parsed.port}` : ""}`);
} else if (parsed.hostname === "127.0.0.1") {
origins.add(`${parsed.protocol}//localhost${parsed.port ? `:${parsed.port}` : ""}`);
}
} catch {
// Ignore malformed dev server URLs and preserve any explicit env configuration.
}
return origins.size > 0 ? Array.from(origins).join(",") : undefined;
}
function toTrimmedString(value: unknown): string | null { function toTrimmedString(value: unknown): string | null {
if (typeof value !== "string") { if (typeof value !== "string") {
@@ -246,12 +221,11 @@ function resolveDesktopAppVersion(): string {
function resolveStatus(): DesktopDaemonStatus { function resolveStatus(): DesktopDaemonStatus {
const home = getPaseoHome(); const home = getPaseoHome();
const config = loadConfig(home, { env: process.env });
const pidPath = pidFilePath(); const pidPath = pidFilePath();
let pid: number | null = null; let pid: number | null = null;
let hostname: string | null = null; let hostname: string | null = null;
let listen: string = config.listen; let listen: string | null = null;
try { try {
if (existsSync(pidPath)) { if (existsSync(pidPath)) {
@@ -266,7 +240,7 @@ function resolveStatus(): DesktopDaemonStatus {
: typeof parsed.sockPath === "string" : typeof parsed.sockPath === "string"
? (parsed.sockPath as string) ? (parsed.sockPath as string)
: null; : null;
if (pidListen) listen = pidListen; listen = pidListen;
} }
} }
} catch { } catch {
@@ -297,18 +271,12 @@ async function startDaemon(): Promise<DesktopDaemonStatus> {
const current = resolveStatus(); const current = resolveStatus();
if (current.status === "running") return current; if (current.status === "running") return current;
const home = getPaseoHome();
const daemonRunner = resolveDaemonRunnerEntrypoint(); const daemonRunner = resolveDaemonRunnerEntrypoint();
const corsOrigins = buildDesktopDaemonCorsOriginsEnv();
const invocation = createNodeEntrypointInvocation({ const invocation = createNodeEntrypointInvocation({
entrypoint: daemonRunner, entrypoint: daemonRunner,
argvMode: "node-script", argvMode: "node-script",
args: [], args: [],
baseEnv: { baseEnv: process.env,
...process.env,
PASEO_HOME: home,
...(corsOrigins ? { PASEO_CORS_ORIGINS: corsOrigins } : {}),
},
}); });
logDesktopDaemonLifecycle("starting detached daemon", { logDesktopDaemonLifecycle("starting detached daemon", {
@@ -317,8 +285,6 @@ async function startDaemon(): Promise<DesktopDaemonStatus> {
daemonRunnerExecArgv: daemonRunner.execArgv, daemonRunnerExecArgv: daemonRunner.execArgv,
command: invocation.command, command: invocation.command,
args: invocation.args, args: invocation.args,
listen: process.env.PASEO_LISTEN ?? null,
corsOrigins: corsOrigins ?? null,
}); });
const child: ChildProcess = spawn( const child: ChildProcess = spawn(
@@ -388,7 +354,7 @@ async function startDaemon(): Promise<DesktopDaemonStatus> {
serverId: status.serverId || null, serverId: status.serverId || null,
}); });
} }
if (status.status === "running" && status.serverId) return status; if (status.status === "running" && status.serverId && status.listen) return status;
await sleep(STARTUP_POLL_INTERVAL_MS); await sleep(STARTUP_POLL_INTERVAL_MS);
} }
@@ -439,6 +405,9 @@ async function getDaemonPairing(): Promise<DesktopPairingOffer> {
} }
try { try {
if (!status.listen) {
throw new Error("Daemon listen target is unavailable.");
}
const baseUrl = buildDaemonHttpBaseUrl(status.listen); const baseUrl = buildDaemonHttpBaseUrl(status.listen);
if (!baseUrl) { if (!baseUrl) {
throw new Error(`Daemon listen target is not a TCP endpoint: ${status.listen}`); throw new Error(`Daemon listen target is not a TCP endpoint: ${status.listen}`);
@@ -480,6 +449,10 @@ async function getLocalDaemonVersion(): Promise<{
}; };
} }
if (!status.listen) {
return { version: null, error: "Daemon listen target is unavailable." };
}
const baseUrl = buildDaemonHttpBaseUrl(status.listen); const baseUrl = buildDaemonHttpBaseUrl(status.listen);
if (!baseUrl) { if (!baseUrl) {
return { version: null, error: `Daemon listen target is not a TCP endpoint: ${status.listen}` }; return { version: null, error: `Daemon listen target is not a TCP endpoint: ${status.listen}` };
+8 -3
View File
@@ -107,7 +107,7 @@ export function resolveDaemonRunnerEntrypoint(): NodeEntrypointSpec {
"server", "server",
"dist", "dist",
"scripts", "scripts",
"daemon-runner.js", "supervisor-entrypoint.js",
), ),
}), }),
execArgv: [], execArgv: [],
@@ -115,7 +115,12 @@ export function resolveDaemonRunnerEntrypoint(): NodeEntrypointSpec {
} }
const serverPackage = resolveServerPackageInfo(); const serverPackage = resolveServerPackageInfo();
const distRunner = path.join(serverPackage.root, "dist", "scripts", "daemon-runner.js"); const distRunner = path.join(
serverPackage.root,
"dist",
"scripts",
"supervisor-entrypoint.js",
);
if (existsSync(distRunner)) { if (existsSync(distRunner)) {
return { return {
entryPath: distRunner, entryPath: distRunner,
@@ -126,7 +131,7 @@ export function resolveDaemonRunnerEntrypoint(): NodeEntrypointSpec {
return { return {
entryPath: assertPathExists({ entryPath: assertPathExists({
label: "Daemon runner source", label: "Daemon runner source",
filePath: path.join(serverPackage.root, "scripts", "daemon-runner.ts"), filePath: path.join(serverPackage.root, "scripts", "supervisor-entrypoint.ts"),
}), }),
execArgv: ["--import", "tsx"], execArgv: ["--import", "tsx"],
}; };
+1 -1
View File
@@ -7,7 +7,7 @@ dotenv.config({
quiet: true, quiet: true,
}); });
const daemonRunnerEntry = fileURLToPath(new URL("./daemon-runner.ts", import.meta.url)); const daemonRunnerEntry = fileURLToPath(new URL("./supervisor-entrypoint.ts", import.meta.url));
const result = spawnSync( const result = spawnSync(
process.execPath, process.execPath,
["--inspect", "--heapsnapshot-near-heap-limit=3", "--max-old-space-size=3072", "--report-on-fatalerror", "--report-directory=/tmp/paseo-reports", ...process.execArgv, daemonRunnerEntry, "--dev", ...process.argv.slice(2)], ["--inspect", "--heapsnapshot-near-heap-limit=3", "--max-old-space-size=3072", "--report-on-fatalerror", "--report-directory=/tmp/paseo-reports", ...process.execArgv, daemonRunnerEntry, "--dev", ...process.argv.slice(2)],
@@ -3,7 +3,10 @@ import { describe, expect, test } from "vitest";
describe("supervision parity", () => { describe("supervision parity", () => {
test("has exactly one runtime callsite for runSupervisor", () => { test("has exactly one runtime callsite for runSupervisor", () => {
const daemonRunner = readFileSync(new URL("./daemon-runner.ts", import.meta.url), "utf8"); const daemonRunner = readFileSync(
new URL("./supervisor-entrypoint.ts", import.meta.url),
"utf8",
);
const devRunner = readFileSync(new URL("./dev-runner.ts", import.meta.url), "utf8"); const devRunner = readFileSync(new URL("./dev-runner.ts", import.meta.url), "utf8");
const daemonRunnerCalls = (daemonRunner.match(/\brunSupervisor\s*\(/g) ?? []).length; const daemonRunnerCalls = (daemonRunner.match(/\brunSupervisor\s*\(/g) ?? []).length;
@@ -1,8 +1,12 @@
import { fileURLToPath } from "url"; import { fileURLToPath } from "url";
import { existsSync } from "node:fs"; import { existsSync } from "node:fs";
import path from "node:path"; import path from "node:path";
import { loadConfig } from "../src/server/config.js"; import {
import { acquirePidLock, PidLockError, releasePidLock } from "../src/server/pid-lock.js"; acquirePidLock,
PidLockError,
releasePidLock,
updatePidLock,
} from "../src/server/pid-lock.js";
import { resolvePaseoHome } from "../src/server/paseo-home.js"; import { resolvePaseoHome } from "../src/server/paseo-home.js";
import { runSupervisor } from "./supervisor.js"; import { runSupervisor } from "./supervisor.js";
import { applySherpaLoaderEnv } from "../src/server/speech/providers/local/sherpa/sherpa-runtime-env.js"; import { applySherpaLoaderEnv } from "../src/server/speech/providers/local/sherpa/sherpa-runtime-env.js";
@@ -72,10 +76,7 @@ async function main(): Promise<void> {
const config = parseConfig(process.argv.slice(2)); const config = parseConfig(process.argv.slice(2));
const workerEntry = config.devMode ? resolveDevWorkerEntry() : resolveWorkerEntry(); const workerEntry = config.devMode ? resolveDevWorkerEntry() : resolveWorkerEntry();
const workerExecArgv = resolveWorkerExecArgv(workerEntry); const workerExecArgv = resolveWorkerExecArgv(workerEntry);
const workerEnv: NodeJS.ProcessEnv = { const workerEnv: NodeJS.ProcessEnv = { ...process.env };
...process.env,
PASEO_PID_LOCK_MODE: "external",
};
const packagedNodeEntrypointRunner = const packagedNodeEntrypointRunner =
process.env.ELECTRON_RUN_AS_NODE === "1" process.env.ELECTRON_RUN_AS_NODE === "1"
? resolvePackagedNodeEntrypointRunnerPath(fileURLToPath(import.meta.url)) ? resolvePackagedNodeEntrypointRunnerPath(fileURLToPath(import.meta.url))
@@ -84,10 +85,9 @@ async function main(): Promise<void> {
applySherpaLoaderEnv(workerEnv); applySherpaLoaderEnv(workerEnv);
const paseoHome = resolvePaseoHome(workerEnv); const paseoHome = resolvePaseoHome(workerEnv);
const daemonConfig = loadConfig(paseoHome, { env: workerEnv });
try { try {
await acquirePidLock(paseoHome, daemonConfig.listen, { await acquirePidLock(paseoHome, null, {
ownerPid: process.pid, ownerPid: process.pid,
}); });
} catch (error) { } catch (error) {
@@ -135,6 +135,9 @@ async function main(): Promise<void> {
}) })
: undefined, : undefined,
restartOnCrash: config.devMode, restartOnCrash: config.devMode,
onWorkerReady: async ({ listen }) => {
await updatePidLock(paseoHome, { listen }, { ownerPid: process.pid });
},
onSupervisorExit: releaseLock, onSupervisorExit: releaseLock,
}); });
} }
+27 -1
View File
@@ -4,6 +4,10 @@ type WorkerLifecycleMessage =
| { | {
type: "paseo:shutdown"; type: "paseo:shutdown";
} }
| {
type: "paseo:ready";
listen: string;
}
| { | {
type: "paseo:restart"; type: "paseo:restart";
reason?: string; reason?: string;
@@ -21,6 +25,7 @@ type SupervisorOptions = {
args: string[]; args: string[];
env?: NodeJS.ProcessEnv; env?: NodeJS.ProcessEnv;
} | null; } | null;
onWorkerReady?: (message: { listen: string }) => Promise<void> | void;
restartOnCrash?: boolean; restartOnCrash?: boolean;
onSupervisorExit?: () => Promise<void> | void; onSupervisorExit?: () => Promise<void> | void;
}; };
@@ -37,6 +42,13 @@ function parseLifecycleMessage(msg: unknown): WorkerLifecycleMessage | null {
if (type === "paseo:shutdown") { if (type === "paseo:shutdown") {
return { type: "paseo:shutdown" }; return { type: "paseo:shutdown" };
} }
if (type === "paseo:ready") {
const listen = (msg as { listen?: unknown }).listen;
if (typeof listen !== "string" || listen.trim().length === 0) {
return null;
}
return { type: "paseo:ready", listen };
}
if (type === "paseo:restart") { if (type === "paseo:restart") {
const reason = (msg as { reason?: unknown }).reason; const reason = (msg as { reason?: unknown }).reason;
return { return {
@@ -110,6 +122,16 @@ export function runSupervisor(options: SupervisorOptions): void {
return; return;
} }
if (lifecycleMessage.type === "paseo:ready") {
Promise.resolve(options.onWorkerReady?.({ listen: lifecycleMessage.listen })).catch(
(error) => {
const message = error instanceof Error ? error.message : String(error);
log(`Worker ready callback failed: ${message}`);
},
);
return;
}
if (lifecycleMessage.type === "paseo:shutdown") { if (lifecycleMessage.type === "paseo:shutdown") {
requestShutdown("Shutdown requested by worker"); requestShutdown("Shutdown requested by worker");
return; return;
@@ -127,7 +149,11 @@ export function runSupervisor(options: SupervisorOptions): void {
return; return;
} }
if (restarting || (restartOnCrash && code !== 0 && code !== null)) { const crashed =
restartOnCrash &&
((code !== 0 && code !== null) || (signal !== null && signal === "SIGKILL"));
if (restarting || crashed) {
restarting = false; restarting = false;
log(`Worker exited (${exitDescriptor}). Restarting worker...`); log(`Worker exited (${exitDescriptor}). Restarting worker...`);
spawnWorker(); spawnWorker();
+10 -27
View File
@@ -112,7 +112,6 @@ import { getOrCreateServerId } from "./server-id.js";
import { resolveDaemonVersion } from "./daemon-version.js"; import { resolveDaemonVersion } from "./daemon-version.js";
import type { AgentClient, AgentProvider } from "./agent/agent-sdk-types.js"; import type { AgentClient, AgentProvider } from "./agent/agent-sdk-types.js";
import type { AgentProviderRuntimeSettingsMap } from "./agent/provider-launch-config.js"; import type { AgentProviderRuntimeSettingsMap } from "./agent/provider-launch-config.js";
import { acquirePidLock, releasePidLock } from "./pid-lock.js";
import { isHostAllowed, type AllowedHostsConfig } from "./allowed-hosts.js"; import { isHostAllowed, type AllowedHostsConfig } from "./allowed-hosts.js";
import { import {
createVoiceMcpSocketBridgeManager, createVoiceMcpSocketBridgeManager,
@@ -183,10 +182,6 @@ export type PaseoDaemonConfig = {
downloadTokenTtlMs?: number; downloadTokenTtlMs?: number;
agentProviderSettings?: AgentProviderRuntimeSettingsMap; agentProviderSettings?: AgentProviderRuntimeSettingsMap;
onLifecycleIntent?: (intent: DaemonLifecycleIntent) => void; onLifecycleIntent?: (intent: DaemonLifecycleIntent) => void;
pidLock?: {
mode?: "self" | "external";
ownerPid?: number;
};
}; };
export interface PaseoDaemon { export interface PaseoDaemon {
@@ -207,17 +202,6 @@ export async function createPaseoDaemon(
const bootstrapStart = performance.now(); const bootstrapStart = performance.now();
const elapsed = () => `${(performance.now() - bootstrapStart).toFixed(0)}ms`; const elapsed = () => `${(performance.now() - bootstrapStart).toFixed(0)}ms`;
const daemonVersion = resolveDaemonVersion(import.meta.url); const daemonVersion = resolveDaemonVersion(import.meta.url);
const pidLockMode = config.pidLock?.mode ?? "self";
const pidLockOwnerPid = config.pidLock?.ownerPid;
const ownsPidLock = pidLockMode === "self";
// Acquire PID lock before expensive bootstrap work so duplicate starts fail immediately.
if (ownsPidLock) {
await acquirePidLock(config.paseoHome, config.listen, {
ownerPid: pidLockOwnerPid,
});
logger.info({ elapsed: elapsed() }, "PID lock acquired");
}
try { try {
const serverId = getOrCreateServerId(config.paseoHome, { logger }); const serverId = getOrCreateServerId(config.paseoHome, { logger });
@@ -707,6 +691,16 @@ export async function createPaseoDaemon(
); );
} }
if (typeof process.send === "function") {
process.send({
type: "paseo:ready",
listen:
boundListenTarget.type === "tcp"
? `${boundListenTarget.host}:${boundListenTarget.port}`
: boundListenTarget.path,
});
}
if (relayEnabled) { if (relayEnabled) {
const offer = await createConnectionOfferV2({ const offer = await createConnectionOfferV2({
serverId, serverId,
@@ -776,12 +770,6 @@ export async function createPaseoDaemon(
if (listenTarget.type === "socket" && existsSync(listenTarget.path)) { if (listenTarget.type === "socket" && existsSync(listenTarget.path)) {
unlinkSync(listenTarget.path); unlinkSync(listenTarget.path);
} }
// Release PID lock
if (ownsPidLock) {
await releasePidLock(config.paseoHome, {
ownerPid: pidLockOwnerPid,
});
}
}; };
return { return {
@@ -794,11 +782,6 @@ export async function createPaseoDaemon(
getListenTarget: () => boundListenTarget, getListenTarget: () => boundListenTarget,
}; };
} catch (err) { } catch (err) {
if (ownsPidLock) {
await releasePidLock(config.paseoHome, {
ownerPid: pidLockOwnerPid,
}).catch(() => undefined);
}
throw err; throw err;
} }
} }
-4
View File
@@ -131,14 +131,10 @@ async function main() {
}; };
try { try {
const pidLockMode = process.env.PASEO_PID_LOCK_MODE === "external" ? "external" : "self";
daemon = await createPaseoDaemon( daemon = await createPaseoDaemon(
{ {
...config, ...config,
onLifecycleIntent: handleLifecycleIntent, onLifecycleIntent: handleLifecycleIntent,
pidLock: {
mode: pidLockMode,
},
}, },
logger, logger,
); );
+15 -3
View File
@@ -3,7 +3,7 @@ import { tmpdir } from "node:os";
import { join } from "node:path"; import { join } from "node:path";
import { describe, expect, test } from "vitest"; import { describe, expect, test } from "vitest";
import { acquirePidLock, getPidLockInfo, releasePidLock } from "./pid-lock.js"; import { acquirePidLock, getPidLockInfo, releasePidLock, updatePidLock } from "./pid-lock.js";
describe("pid-lock ownership", () => { describe("pid-lock ownership", () => {
test("writes and releases lock for explicit owner pid", async () => { test("writes and releases lock for explicit owner pid", async () => {
@@ -14,13 +14,25 @@ describe("pid-lock ownership", () => {
await ( await (
acquirePidLock as unknown as ( acquirePidLock as unknown as (
home: string, home: string,
sockPath: string, sockPath: string | null,
options: { ownerPid: number }, options: { ownerPid: number },
) => Promise<void> ) => Promise<void>
)(paseoHome, "127.0.0.1:6767", { ownerPid }); )(paseoHome, null, { ownerPid });
const lock = await getPidLockInfo(paseoHome); const lock = await getPidLockInfo(paseoHome);
expect(lock?.pid).toBe(ownerPid); expect(lock?.pid).toBe(ownerPid);
expect(lock?.listen).toBeNull();
await (
updatePidLock as unknown as (
home: string,
patch: { listen: string },
options: { ownerPid: number },
) => Promise<void>
)(paseoHome, { listen: "127.0.0.1:6767" }, { ownerPid });
const updatedLock = await getPidLockInfo(paseoHome);
expect(updatedLock?.listen).toBe("127.0.0.1:6767");
await ( await (
releasePidLock as unknown as (home: string, options: { ownerPid: number }) => Promise<void> releasePidLock as unknown as (home: string, options: { ownerPid: number }) => Promise<void>
+33 -2
View File
@@ -8,7 +8,7 @@ export interface PidLockInfo {
startedAt: string; startedAt: string;
hostname: string; hostname: string;
uid: number; uid: number;
listen: string; listen: string | null;
} }
export class PidLockError extends Error { export class PidLockError extends Error {
@@ -43,7 +43,7 @@ function resolveOwnerPid(ownerPid?: number): number {
export async function acquirePidLock( export async function acquirePidLock(
paseoHome: string, paseoHome: string,
listen: string, listen: string | null,
options?: { ownerPid?: number }, options?: { ownerPid?: number },
): Promise<void> { ): Promise<void> {
const pidPath = getPidFilePath(paseoHome); const pidPath = getPidFilePath(paseoHome);
@@ -114,6 +114,37 @@ export async function acquirePidLock(
} }
} }
export async function updatePidLock(
paseoHome: string,
patch: { listen: string },
options?: { ownerPid?: number },
): Promise<void> {
const pidPath = getPidFilePath(paseoHome);
const lockOwnerPid = resolveOwnerPid(options?.ownerPid);
const content = await readFile(pidPath, "utf-8");
const existingLock = JSON.parse(content) as PidLockInfo;
if (existingLock.pid !== lockOwnerPid) {
throw new PidLockError(
`Cannot update PID lock owned by PID ${existingLock.pid}`,
existingLock,
);
}
const updatedLock: PidLockInfo = {
...existingLock,
...patch,
};
const fd = await open(pidPath, "r+");
try {
await fd.truncate(0);
await fd.writeFile(JSON.stringify(updatedLock));
} finally {
await fd.close();
}
}
export async function releasePidLock( export async function releasePidLock(
paseoHome: string, paseoHome: string,
options?: { ownerPid?: number }, options?: { ownerPid?: number },
+8 -2
View File
@@ -1438,7 +1438,10 @@ export class Session {
this.peakInflightRequests = this.inflightRequests; this.peakInflightRequests = this.inflightRequests;
} }
try { try {
this.sessionLogger.trace({ inbound: msg }, "inbound message"); this.sessionLogger.trace(
{ messageType: msg.type, payloadBytes: JSON.stringify(msg).length },
"inbound message",
);
try { try {
switch (msg.type) { switch (msg.type) {
case "voice_audio_chunk": case "voice_audio_chunk":
@@ -7119,7 +7122,10 @@ export class Session {
* Emit a message to the client * Emit a message to the client
*/ */
private emit(msg: SessionOutboundMessage): void { private emit(msg: SessionOutboundMessage): void {
this.sessionLogger.trace({ outbound: msg }, "outbound message"); this.sessionLogger.trace(
{ messageType: msg.type, payloadBytes: JSON.stringify(msg).length },
"outbound message",
);
if ( if (
msg.type === "audio_output" && msg.type === "audio_output" &&
(process.env.TTS_DEBUG_AUDIO_DIR || isPaseoDictationDebugEnabled()) && (process.env.TTS_DEBUG_AUDIO_DIR || isPaseoDictationDebugEnabled()) &&
+1 -1
View File
@@ -19,6 +19,6 @@
"declaration": false, "declaration": false,
"sourceMap": true "sourceMap": true
}, },
"include": ["scripts/daemon-runner.ts", "scripts/dev-runner.ts", "scripts/supervisor.ts"], "include": ["scripts/supervisor-entrypoint.ts", "scripts/dev-runner.ts", "scripts/supervisor.ts"],
"exclude": ["node_modules", "dist"] "exclude": ["node_modules", "dist"]
} }