Files
orca/src/main/runtime/runtime-metadata-ownership-watch.ts
T
Brennan Benson c75c04eaae fix(runtime): reclaim orca-runtime.json when it stops describing this runtime (#10840)
* fix(runtime): reclaim orca-runtime.json when it stops describing this runtime

On macOS the Chromium single-instance lock is silently defeated whenever
`SingletonSocket`/`SingletonCookie` go missing from the profile — and the
socket they point at lives under `$TMPDIR` (`/var/folders/.../T`), which
macOS purges after 3 days (`com.apple.bsd.dirhelper`,
CLEAN_FILES_OLDER_THAN_DAYS=3). A launch that slips past the lock runs a
full startup, republishes `orca-runtime.json` with its own pid, and leaves
the CLI on a dead pid once it exits: `orca status` reports
`stale_bootstrap` and every terminal command fails `runtime_unavailable`
while the original app keeps serving.

The owner now watches its own discovery record and republishes once no live
runtime is described. Reclaiming only a dead pid is deliberate: two live
runtimes sharing a profile would otherwise fight over the file.

Reproduced on macOS with two real Orca main processes on one profile: the
second instance took the lock and clobbered the record, and killing it left
`stale_bootstrap` against the still-healthy first instance. With this
change the owner reclaimed the record in ~2s and the CLI returned to
`ready`.

Refs #7848

* test(runtime): assert stop() clears the metadata ownership timer

The republish guard alone kept the shutdown test green, so the watch teardown was unasserted. Also drop the doc claim of startup/activation callers that do not exist.

* test(runtime): stand in a real live pid for the sibling-runtime case

Windows never assigns pid 1, so the hardcoded sibling read as dead there and
the watch would reclaim the record. Own a synthetic pid instead and let
process.pid play the live sibling.
2026-07-27 17:32:55 -07:00

115 lines
3.7 KiB
TypeScript

import { getRuntimeMetadataPath, type RuntimeMetadata } from '../../shared/runtime-bootstrap'
import { readRuntimeMetadata } from './runtime-metadata'
/**
* Why: `orca-runtime.json` is the CLI's only pointer at a live runtime, and it
* can stop describing this process while this process is still serving RPC —
* a second instance that slipped past the single-instance lock publishes its
* own pid and then exits, leaving the CLI on a dead pid (`stale_bootstrap`)
* against a healthy app (#7848). Chromium's lock is defeated on macOS whenever
* `SingletonSocket`/`SingletonCookie` go missing, and its socket lives under
* `$TMPDIR` (`/var/folders/.../T`), which macOS itself purges after 3 days.
*
* The owner therefore watches its own record and reclaims it once no live
* runtime is described. Reclaiming only a dead pid is deliberate: two live
* runtimes sharing a profile would otherwise ping-pong the file forever.
*/
export const RUNTIME_METADATA_OWNERSHIP_POLL_MS = 10_000
export type RuntimeMetadataOwnershipWatch = {
/** Runs one ownership check immediately; exposed for tests and eager repair. */
check: () => void
stop: () => void
}
export type RuntimeMetadataOwnershipWatchOptions = {
userDataPath: string
ownedPid: number
ownedRuntimeId: string
republish: () => void
pollIntervalMs?: number
isProcessRunning?: (pid: number) => boolean
onReclaim?: (previous: RuntimeMetadata | null) => void
}
export function shouldReclaimRuntimeMetadata(
current: RuntimeMetadata | null,
ownedPid: number,
ownedRuntimeId: string,
isProcessRunning: (pid: number) => boolean
): boolean {
if (!current) {
return true
}
if (current.pid === ownedPid && current.runtimeId === ownedRuntimeId) {
return false
}
// Why: only this process can legitimately claim this pid, so a foreign
// runtimeId on it is a leftover from a recycled pid, not a live sibling.
if (current.pid === ownedPid) {
return true
}
return !isProcessRunning(current.pid)
}
export function watchRuntimeMetadataOwnership(
options: RuntimeMetadataOwnershipWatchOptions
): RuntimeMetadataOwnershipWatch {
const isProcessRunning = options.isProcessRunning ?? isPidRunning
const check = (): void => {
const current = tryReadRuntimeMetadata(options.userDataPath)
if (
!shouldReclaimRuntimeMetadata(
current,
options.ownedPid,
options.ownedRuntimeId,
isProcessRunning
)
) {
return
}
try {
options.republish()
} catch (error) {
// Why: a transient write failure must not kill the watch; the next tick retries.
console.error('[runtime] Failed to reclaim runtime metadata ownership:', error)
return
}
options.onReclaim?.(current)
}
const timer = setInterval(check, options.pollIntervalMs ?? RUNTIME_METADATA_OWNERSHIP_POLL_MS)
// Why: discovery bookkeeping must never be the reason the process stays alive.
timer.unref?.()
return {
check,
stop: () => clearInterval(timer)
}
}
function tryReadRuntimeMetadata(userDataPath: string): RuntimeMetadata | null {
try {
return readRuntimeMetadata(userDataPath)
} catch (error) {
// Why: an unparseable record is as useless to the CLI as a missing one, so treat it as reclaimable.
console.warn(
`[runtime] Ignoring unreadable ${getRuntimeMetadataPath(userDataPath)}:`,
error instanceof Error ? error.message : String(error)
)
return null
}
}
function isPidRunning(pid: number): boolean {
if (!pid || pid <= 0) {
return false
}
try {
process.kill(pid, 0)
return true
} catch (error) {
// Why: only ESRCH proves the pid is gone; EPERM means a foreign owner holds it (same rule as the socket sweep).
return (error as NodeJS.ErrnoException).code !== 'ESRCH'
}
}