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
@@ -7,7 +7,7 @@ dotenv.config({
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(
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)],
@@ -3,7 +3,10 @@ import { describe, expect, test } from "vitest";
describe("supervision parity", () => {
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 daemonRunnerCalls = (daemonRunner.match(/\brunSupervisor\s*\(/g) ?? []).length;
@@ -1,8 +1,12 @@
import { fileURLToPath } from "url";
import { existsSync } from "node:fs";
import path from "node:path";
import { loadConfig } from "../src/server/config.js";
import { acquirePidLock, PidLockError, releasePidLock } from "../src/server/pid-lock.js";
import {
acquirePidLock,
PidLockError,
releasePidLock,
updatePidLock,
} from "../src/server/pid-lock.js";
import { resolvePaseoHome } from "../src/server/paseo-home.js";
import { runSupervisor } from "./supervisor.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 workerEntry = config.devMode ? resolveDevWorkerEntry() : resolveWorkerEntry();
const workerExecArgv = resolveWorkerExecArgv(workerEntry);
const workerEnv: NodeJS.ProcessEnv = {
...process.env,
PASEO_PID_LOCK_MODE: "external",
};
const workerEnv: NodeJS.ProcessEnv = { ...process.env };
const packagedNodeEntrypointRunner =
process.env.ELECTRON_RUN_AS_NODE === "1"
? resolvePackagedNodeEntrypointRunnerPath(fileURLToPath(import.meta.url))
@@ -84,10 +85,9 @@ async function main(): Promise<void> {
applySherpaLoaderEnv(workerEnv);
const paseoHome = resolvePaseoHome(workerEnv);
const daemonConfig = loadConfig(paseoHome, { env: workerEnv });
try {
await acquirePidLock(paseoHome, daemonConfig.listen, {
await acquirePidLock(paseoHome, null, {
ownerPid: process.pid,
});
} catch (error) {
@@ -135,6 +135,9 @@ async function main(): Promise<void> {
})
: undefined,
restartOnCrash: config.devMode,
onWorkerReady: async ({ listen }) => {
await updatePidLock(paseoHome, { listen }, { ownerPid: process.pid });
},
onSupervisorExit: releaseLock,
});
}
+27 -1
View File
@@ -4,6 +4,10 @@ type WorkerLifecycleMessage =
| {
type: "paseo:shutdown";
}
| {
type: "paseo:ready";
listen: string;
}
| {
type: "paseo:restart";
reason?: string;
@@ -21,6 +25,7 @@ type SupervisorOptions = {
args: string[];
env?: NodeJS.ProcessEnv;
} | null;
onWorkerReady?: (message: { listen: string }) => Promise<void> | void;
restartOnCrash?: boolean;
onSupervisorExit?: () => Promise<void> | void;
};
@@ -37,6 +42,13 @@ function parseLifecycleMessage(msg: unknown): WorkerLifecycleMessage | null {
if (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") {
const reason = (msg as { reason?: unknown }).reason;
return {
@@ -110,6 +122,16 @@ export function runSupervisor(options: SupervisorOptions): void {
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") {
requestShutdown("Shutdown requested by worker");
return;
@@ -127,7 +149,11 @@ export function runSupervisor(options: SupervisorOptions): void {
return;
}
if (restarting || (restartOnCrash && code !== 0 && code !== null)) {
const crashed =
restartOnCrash &&
((code !== 0 && code !== null) || (signal !== null && signal === "SIGKILL"));
if (restarting || crashed) {
restarting = false;
log(`Worker exited (${exitDescriptor}). Restarting worker...`);
spawnWorker();
+10 -27
View File
@@ -112,7 +112,6 @@ import { getOrCreateServerId } from "./server-id.js";
import { resolveDaemonVersion } from "./daemon-version.js";
import type { AgentClient, AgentProvider } from "./agent/agent-sdk-types.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 {
createVoiceMcpSocketBridgeManager,
@@ -183,10 +182,6 @@ export type PaseoDaemonConfig = {
downloadTokenTtlMs?: number;
agentProviderSettings?: AgentProviderRuntimeSettingsMap;
onLifecycleIntent?: (intent: DaemonLifecycleIntent) => void;
pidLock?: {
mode?: "self" | "external";
ownerPid?: number;
};
};
export interface PaseoDaemon {
@@ -207,17 +202,6 @@ export async function createPaseoDaemon(
const bootstrapStart = performance.now();
const elapsed = () => `${(performance.now() - bootstrapStart).toFixed(0)}ms`;
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 {
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) {
const offer = await createConnectionOfferV2({
serverId,
@@ -776,12 +770,6 @@ export async function createPaseoDaemon(
if (listenTarget.type === "socket" && existsSync(listenTarget.path)) {
unlinkSync(listenTarget.path);
}
// Release PID lock
if (ownsPidLock) {
await releasePidLock(config.paseoHome, {
ownerPid: pidLockOwnerPid,
});
}
};
return {
@@ -794,11 +782,6 @@ export async function createPaseoDaemon(
getListenTarget: () => boundListenTarget,
};
} catch (err) {
if (ownsPidLock) {
await releasePidLock(config.paseoHome, {
ownerPid: pidLockOwnerPid,
}).catch(() => undefined);
}
throw err;
}
}
-4
View File
@@ -131,14 +131,10 @@ async function main() {
};
try {
const pidLockMode = process.env.PASEO_PID_LOCK_MODE === "external" ? "external" : "self";
daemon = await createPaseoDaemon(
{
...config,
onLifecycleIntent: handleLifecycleIntent,
pidLock: {
mode: pidLockMode,
},
},
logger,
);
+15 -3
View File
@@ -3,7 +3,7 @@ import { tmpdir } from "node:os";
import { join } from "node:path";
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", () => {
test("writes and releases lock for explicit owner pid", async () => {
@@ -14,13 +14,25 @@ describe("pid-lock ownership", () => {
await (
acquirePidLock as unknown as (
home: string,
sockPath: string,
sockPath: string | null,
options: { ownerPid: number },
) => Promise<void>
)(paseoHome, "127.0.0.1:6767", { ownerPid });
)(paseoHome, null, { ownerPid });
const lock = await getPidLockInfo(paseoHome);
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 (
releasePidLock as unknown as (home: string, options: { ownerPid: number }) => Promise<void>
+33 -2
View File
@@ -8,7 +8,7 @@ export interface PidLockInfo {
startedAt: string;
hostname: string;
uid: number;
listen: string;
listen: string | null;
}
export class PidLockError extends Error {
@@ -43,7 +43,7 @@ function resolveOwnerPid(ownerPid?: number): number {
export async function acquirePidLock(
paseoHome: string,
listen: string,
listen: string | null,
options?: { ownerPid?: number },
): Promise<void> {
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(
paseoHome: string,
options?: { ownerPid?: number },
+8 -2
View File
@@ -1438,7 +1438,10 @@ export class Session {
this.peakInflightRequests = this.inflightRequests;
}
try {
this.sessionLogger.trace({ inbound: msg }, "inbound message");
this.sessionLogger.trace(
{ messageType: msg.type, payloadBytes: JSON.stringify(msg).length },
"inbound message",
);
try {
switch (msg.type) {
case "voice_audio_chunk":
@@ -7119,7 +7122,10 @@ export class Session {
* Emit a message to the client
*/
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 (
msg.type === "audio_output" &&
(process.env.TTS_DEBUG_AUDIO_DIR || isPaseoDictationDebugEnabled()) &&
+1 -1
View File
@@ -19,6 +19,6 @@
"declaration": false,
"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"]
}