Files
orca/src/main/daemon/daemon-spawner.ts
T
Jinwoo-HandOrca a1b1b1439a fix(daemon): guard killStaleDaemon against pid recycling
The daemon's pid file carried only a bare integer, so killStaleDaemon had
no way to verify the current owner of that pid was still the process it
forked. Unix pids recycle — on macOS the default ceiling is ~2048 and
wraps in minutes under load — so blind SIGTERM/SIGKILL on a recycled pid
could hit an unrelated user process (editor, language server, background
task) silently.

Three moving pieces:

1. Pid file now carries startedAtMs. daemon-spawner exports DaemonPidFile
   and serializeDaemonPidFile; daemon-init fills startedAtMs from
   getProcessStartedAtMs(child.pid) right after the daemon signals ready.
   parseDaemonPidFile tries JSON first and falls back to bare-integer for
   backward compatibility (startedAtMs: null on legacy files).

2. isDaemonProcess takes expected startedAtMs. In addition to the cmdline
   match, it consults getProcessStartedAtMs(pid) and compares with a
   ±1.5s tolerance. Null on either side is fail-open so the guard never
   strengthens existing behavior negatively (Windows, legacy pid files,
   kernel-thread /proc failures).

3. SIGKILL re-check. The SIGTERM-then-wait window is up to 3s — long
   enough for the pid to be recycled if the original daemon dies during
   the wait. Before escalating to SIGKILL, isDaemonProcess runs again;
   on mismatch we log reason=pid_recycled and skip SIGKILL.

Carried forward from #1323 as a standalone safety fix — independent of
that PR's router/drain machinery, which is not shipping.

Co-authored-by: Orca <help@stably.ai>
2026-05-02 00:40:45 -04:00

92 lines
2.7 KiB
TypeScript

import { createHash } from 'crypto'
import { join } from 'path'
import { PROTOCOL_VERSION } from './types'
export type DaemonConnectionInfo = {
socketPath: string
tokenPath: string
}
export type DaemonPidFile = {
pid: number
startedAtMs: number | null
}
export type DaemonProcessHandle = {
shutdown(): Promise<void>
}
export type DaemonLauncher = (socketPath: string, tokenPath: string) => Promise<DaemonProcessHandle>
export type DaemonSpawnerOptions = {
runtimeDir: string
launcher: DaemonLauncher
}
export class DaemonSpawner {
private runtimeDir: string
private launcher: DaemonLauncher
private handle: DaemonProcessHandle | null = null
private socketPath: string
private tokenPath: string
constructor(opts: DaemonSpawnerOptions) {
this.runtimeDir = opts.runtimeDir
this.launcher = opts.launcher
this.socketPath = getDaemonSocketPath(this.runtimeDir)
this.tokenPath = getDaemonTokenPath(this.runtimeDir)
}
async ensureRunning(): Promise<DaemonConnectionInfo> {
if (this.handle) {
return { socketPath: this.socketPath, tokenPath: this.tokenPath }
}
this.handle = await this.launcher(this.socketPath, this.tokenPath)
return { socketPath: this.socketPath, tokenPath: this.tokenPath }
}
// Why: after the daemon process dies unexpectedly, the cached handle is
// stale. Clearing it lets the next ensureRunning() fork a fresh daemon
// instead of returning the dead socket path.
resetHandle(): void {
this.handle = null
}
async shutdown(): Promise<void> {
if (!this.handle) {
return
}
const handle = this.handle
this.handle = null
await handle.shutdown()
}
}
export function getDaemonSocketPath(
runtimeDir: string,
protocolVersion = PROTOCOL_VERSION
): string {
// Why: Windows IPC servers use named pipes rather than filesystem socket
// files. Include the protocol version in the endpoint name so a daemon from
// an older build is never reused after a breaking protocol change.
if (process.platform === 'win32') {
const suffix = createHash('sha256').update(runtimeDir).digest('hex').slice(0, 12)
return `\\\\?\\pipe\\orca-terminal-host-v${protocolVersion}-${suffix}`
}
return join(runtimeDir, `daemon-v${protocolVersion}.sock`)
}
export function getDaemonTokenPath(runtimeDir: string, protocolVersion = PROTOCOL_VERSION): string {
return join(runtimeDir, `daemon-v${protocolVersion}.token`)
}
export function getDaemonPidPath(runtimeDir: string, protocolVersion = PROTOCOL_VERSION): string {
return join(runtimeDir, `daemon-v${protocolVersion}.pid`)
}
export function serializeDaemonPidFile(pidFile: DaemonPidFile): string {
return JSON.stringify(pidFile)
}