Files
orca/src/main/ipc/parcel-watcher-process.ts
T
Brennan Benson 64be819790 fix(runtime): harden watcher and PTY teardown ownership (#8661)
* fix(runtime): retain watcher and PTY teardown ownership

* fix(runtime): restore watchers after interrupted cleanup

* fix(runtime): prevent stale watcher revival

* test(runtime): cover watcher shutdown ownership

* test(daemon): model physical PTY exit

* fix(daemon): keep shutdown terminating when disposal cannot prove exit

A rejecting host.dispose() (unreapable child past its exit deadline) left
the shutdown RPC without its process.nextTick(shutdown) and skipped socket
cleanup in shutdown(), stranding the daemon as an unreachable orphan after
the stale-daemon replacement flow unlinks its socket. Log and continue:
daemon exit reparents the child to init instead of blocking on it.

* fix(runtime): keep local watching alive after an idle-kill deadline miss

An idle child that outlived the exit deadline set shutdownRequested on the
shared desktop supervisor, which has no retire-and-replace path — every
later subscribe rejected supervisor_disposed and the roots were cached
unwatchable, silently ending local file watching for the session. The idle
path owns zero records, so there is no double-watch hazard; the zombie
keeps its capacity reservation until physical exit and the next subscribe
gets a fresh child.

* fix(renderer): resync replayed paired-web file watches

Transparent replay removed the implicit resync the old close-and-rebuild
path provided: a replayed files.watch only reports changes from its own
native setup, so changes during the reconnect gap were silently lost.
Deliver a conservative overflow to consumers once the replayed watch is
ready, matching the overflow-after-interruption contract everywhere else.

* fix(runtime): address teardown review findings

* fix(runtime): retry watches after teardown deadlines

* Fix PTY descendant leaks on forced teardown

* Fix jitter-sensitive terminal lifecycle test
2026-07-15 15:23:35 -07:00

79 lines
3.0 KiB
TypeScript

// Host API for crash-isolated @parcel/watcher children. Desktop uses one
// supervisor; runtime roots use a bounded pool with independent crash fuses.
import { WatcherProcessSupervisor } from './parcel-watcher-process-supervisor'
import type { WatcherProcessSubscribeOptions } from './parcel-watcher-process-protocol'
import type {
WatcherProcessCallback,
WatcherProcessHooks,
WatcherProcessSubscription
} from './parcel-watcher-process-subscription'
import { RuntimeWatcherProcessPool } from './runtime-watcher-process-pool'
export type {
WatcherProcessCallback,
WatcherProcessHooks,
WatcherProcessSubscription
} from './parcel-watcher-process-subscription'
export type {
WatcherProcessDeliveryOptions,
WatcherProcessEvent,
WatcherProcessSubscribeOptions
} from './parcel-watcher-process-protocol'
const sharedWatcherProcessSupervisor = new WatcherProcessSupervisor()
// Why: the canary pauses during legitimate crawls, so production callers need
// a separate liveness fuse once a child reports that it owns native setup.
export const WATCHER_PROCESS_SUBSCRIBE_TIMEOUT_MS = 60_000
// Why: healthy roots share one child; only fault quarantine scales to four,
// containing a failed shard without paying that RSS cost during normal use.
const runtimeWatcherProcessPool = new RuntimeWatcherProcessPool()
export function createWatcherProcessSupervisor(): WatcherProcessSupervisor {
return new WatcherProcessSupervisor()
}
export function subscribeViaWatcherProcess(
dir: string,
callback: WatcherProcessCallback,
opts: WatcherProcessSubscribeOptions,
hooks: WatcherProcessHooks = {}
): Promise<WatcherProcessSubscription> {
return sharedWatcherProcessSupervisor.subscribe(dir, callback, opts, hooks)
}
export function subscribeViaRuntimeWatcherProcess(
dir: string,
callback: WatcherProcessCallback,
opts: WatcherProcessSubscribeOptions,
hooks: WatcherProcessHooks = {}
): Promise<WatcherProcessSubscription> {
return runtimeWatcherProcessPool.subscribe(dir, callback, opts, hooks)
}
/** Kill shared desktop and runtime watcher children at app/runtime shutdown. */
export function disposeWatcherProcess(): void {
// Why: Vitest reuses this module singleton after closeAllWatchers(), while
// production shutdown must remain final and reject any later subscription.
if (process.env.VITEST) {
sharedWatcherProcessSupervisor.resetForTest()
runtimeWatcherProcessPool.resetForTest()
} else {
sharedWatcherProcessSupervisor.dispose()
// Why: the runtime pool owns independent children that otherwise outlive a
// shutdown sequence that does not immediately exit the main process.
runtimeWatcherProcessPool.dispose()
}
}
export function resetWatcherProcessForTest(): void {
sharedWatcherProcessSupervisor.resetForTest()
}
export function resetRuntimeWatcherProcessForTest(): void {
runtimeWatcherProcessPool.resetForTest()
}
export function forgetRuntimeWatcherProcessRoot(rootPath: string): void {
runtimeWatcherProcessPool.forgetRoot(rootPath)
}