Files
deepseek-harness/packages/boot/plugin-manager/src/run-tree.ts
T
Turtle ccaa0dc11c fix(plugin-manager): bound pnpm runs so a silent child cannot hold the profile lock (#4982)
* fix(plugin-manager): bound pnpm runs so a silent child cannot hold the profile lock

A pnpm child that prints its completion line and then never exits holds the
profile write lock until the host is restarted, so every other session and host
on that profile fails after its own lock timeout (issue #4981). execa resolves
its promise only once the piped stdio has ended, so a run now completes on the
process's own exit and drains the pipes under a bound instead of awaiting their
end.

A service run whose captured output stays silent for idleTimeoutMs is
terminated, reports timedOut alongside its exit status, and is not retried on
the next registry; a run with inherited descriptors captures nothing and is
left unbounded for its operator.

* test(plugin-manager): type the mocked child and await the drain descendants

The inherited-descriptor fixture no longer needs its legacy `as unknown` cast,
because the mocked child is typed for the fields the operations read; the
retired baseline entry is pruned (verify-no-unknown-casts).

The drain specs wait for the killed descendant to exit before removing its
directory: Windows refuses to delete a directory a live process still holds.

* test(plugin-manager): stop the drain descendant deterministically

The descendant that holds the pipes now ends on a stop file the teardown
writes, and on its own after a minute, so the fixture leaves no process behind
when a kill cannot be delivered. Windows stops it through taskkill, and the
spec waits for the exit before removing the directory it ran in.

* test(plugin-manager): run the drain descendant from its own script file

Windows cannot carry a nested `-e` script through another spawn argument: the
descendant failed to start there, so the fixture asserted on a process that was
already gone. The descendant now runs from a script file the fixture writes.

* test(plugin-manager): cover the bounded drain everywhere and stage the real descendant on POSIX

Windows does not stage a descendant that keeps the inherited pipes open past
the direct child's exit, so the real-process cases run on POSIX only. The
mocked child now models execa's own completion rule — its promise settles only
once both pipes end — which covers the bounded drain on every platform and
still fails without the raw-exit race.

* test(plugin-manager): cover the drain failure path without a child process

An output consumer failure that the bounded drain cuts short is staged with the
mocked child as well, so the per-file coverage gate also holds where the
real-process cases are skipped.

* fix(plugin-manager): terminate the whole tree of a stalled run

A lifecycle script is a child of pnpm, so signalling pnpm alone left an
approved postinstall writing into the profile after the operation reported a
timeout and released the lock. A stalled run now stops its process group (or
Windows process tree) and waits for it to disappear before the caller restores
the files. The cut tail's close is told apart from a reading failure by where it
happened, so a failure from before the cut still surfaces.

* docs(plugin-manager): regenerate the config catalog after the rebase

The English catalog gains `idleTimeoutMs` from the regenerator, and the Chinese
counterpart carries the same interface line so the pair stays consistent.

* fix(plugin-manager): terminate a run's tree through execa, keeping exit cleanup

Spawning a service run with `detached` made execa skip the handler that kills
the child when the manager exits, so a crashed manager left the pnpm tree
running in the profile. `killDescendants` spawns the run as its own process
group while keeping that cleanup, and it addresses the tree on every kill path,
including cancellation. The module now only decides the probe target and waits
for quiescence.
2026-09-23 16:11:30 +08:00

71 lines
3.0 KiB
TypeScript

/** Waiting for one package run's process tree to disappear before its caller touches the profile. */
/** How long a terminated tree may take to disappear before the caller stops waiting. */
const TREE_WAIT_MS = 5_000
/** Poll cadence while waiting for a terminated tree to disappear. */
const TREE_POLL_MS = 15
/** The process (or process group) one run occupies. */
export interface RunTree {
/** The run's process id, when its spawn published one. */
pid: number | undefined
/** Whether the run leads its own process group, which is what a POSIX group probe addresses. */
grouped: boolean
}
/** Injectable process operations, so every platform's probing is testable on any host. */
export interface RunTreeInternals {
/** Host platform override for target decisions. */
platform?: NodeJS.Platform
/** Liveness probe override, defaulting to `process.kill(target, 0)`. */
alive?: (target: number) => boolean
/** Wait bound override, defaulting to the tree wait constant. */
waitMs?: number
}
/**
* Whether one run leads its own process group, which is the target a POSIX
* liveness probe addresses for the whole tree. A run that captures output is
* spawned as its own group leader, because its tree is terminated as a unit; a
* run that inherits the caller's descriptors keeps the caller's group, so an
* interrupt still reaches it.
* @param execution Whether the run captures output or inherits the caller's descriptors.
* @param platform Host platform deciding how a tree is addressed.
* @returns True when the run leads its own process group.
*/
export function leadsOwnGroup(execution: 'cli' | 'service', platform: NodeJS.Platform = process.platform): boolean {
return execution === 'service' && platform !== 'win32'
}
/** The target a tree probe addresses: a POSIX group when the run leads one, else the process itself. */
function targetOf(pid: number, grouped: boolean, platform: NodeJS.Platform): number {
return platform !== 'win32' && grouped ? -pid : pid
}
/**
* Wait until a terminated run's tree is gone, so the caller restores and
* unlocks the profile only after the scripts it started stopped writing.
* @param tree The run's process id and whether it leads its own group.
* @param internals Injectable process operations.
* @returns Fulfillment once no member remains, or the wait bound elapsed.
*/
export async function awaitTreeGone(tree: RunTree, internals: RunTreeInternals = {}): Promise<void> {
const pid = tree.pid
if (pid === undefined) return
const platform = internals.platform ?? process.platform
const alive = internals.alive ?? ((target: number) => { process.kill(target, 0); return true })
const remaining = (): boolean => {
try {
return alive(targetOf(pid, tree.grouped, platform))
} catch {
return false
}
}
const deadline = Date.now() + (internals.waitMs ?? TREE_WAIT_MS)
while (remaining()) {
if (Date.now() >= deadline) return
await new Promise(resolve => setTimeout(resolve, TREE_POLL_MS))
}
}