Files
orca/src/main/ipc/wsl-watcher-process-exit.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

107 lines
2.7 KiB
TypeScript

import type { ChildProcessWithoutNullStreams } from 'node:child_process'
import { PhysicalExitTracker } from '../../shared/physical-exit-tracker'
import { WatcherProcessFailure } from './parcel-watcher-process-failure'
export const WSL_WATCHER_PHYSICAL_EXIT_TIMEOUT_MS = 8_000
export function createWslWatcherStartup(): {
ready: Promise<void>
readonly settled: boolean
settle: (error?: Error) => void
} {
let settled = false
let resolve!: () => void
let reject!: (error: Error) => void
const ready = new Promise<void>((done, fail) => {
resolve = done
reject = fail
})
return {
ready,
get settled() {
return settled
},
settle(error) {
if (settled) {
return
}
settled = true
if (error) {
reject(error)
} else {
resolve()
}
}
}
}
export function createWslWatcherProcessExit(
child: ChildProcessWithoutNullStreams,
worktreePath: string,
onPhysicalExit?: () => void
): {
markPhysicalExit: () => void
requestStopBestEffort: () => void
stopAndWait: () => Promise<void>
} {
let physicalExited = false
let stopRequested = false
const physicalExit = new PhysicalExitTracker()
const markPhysicalExit = (): void => {
if (!physicalExited) {
physicalExited = true
physicalExit.markExited()
onPhysicalExit?.()
}
}
const requestStop = (): void => {
if (physicalExited || stopRequested) {
return
}
stopRequested = true
try {
if (!child.kill()) {
throw new Error('WSL watcher process rejected the termination signal')
}
} catch (error) {
stopRequested = false
throw error
}
}
const requestStopBestEffort = (): void => {
try {
requestStop()
} catch {
// The awaited owner retries and retains the child on failure.
}
}
const stopAndWait = async (): Promise<void> => {
try {
requestStop()
} catch (error) {
throw processExitFailure(
`Failed to terminate WSL watcher: ${String(error)}`,
physicalExit.exitedPromise
)
}
await waitForPhysicalExit(worktreePath, physicalExit)
}
return { markPhysicalExit, requestStopBestEffort, stopAndWait }
}
function waitForPhysicalExit(
worktreePath: string,
physicalExit: PhysicalExitTracker
): Promise<void> {
return physicalExit.waitForExit(WSL_WATCHER_PHYSICAL_EXIT_TIMEOUT_MS, () =>
processExitFailure(
`WSL watcher process did not exit after deadline: ${worktreePath}`,
physicalExit.exitedPromise
)
)
}
function processExitFailure(message: string, physicalExit: Promise<void>): WatcherProcessFailure {
return new WatcherProcessFailure(message, 'supervisor', 'process_unavailable', physicalExit)
}