Files
hapi/cli/src/utils/windowsProcessStop.ts
T
AnanovoandGitHub 8c602eb7a1 fix(cli,hub): prevent premature archive failures on Windows (#2126)
* fix(cli,hub): prevent premature archive failures on Windows

* fix(cli): preserve tree-wide forced shutdown escalation

* fix(cli): stop disconnected survivor subtrees completely

* test(hub): assert structured archive cleanup uncertainty

* fix(cli): use invariant Windows process markers

* fix(cli): attempt Windows termination when CIM fails
2026-11-02 18:03:26 +08:00

239 lines
12 KiB
TypeScript

import { execFile } from 'node:child_process'
export type WindowsProcessEntry = { parentPid: number; marker: string | null; name: string; commandLine: string }
export type WindowsProcessTable = Map<number, WindowsProcessEntry>
// One complete CIM snapshot supplies ancestry AND generation markers. Launching
// PowerShell/tasklist for each member made a small tree take over a minute.
export function windowsProcessTableCommand(pids?: number[], includeCommandLine = false): string {
if (pids?.length && !includeCommandLine) {
// Verification needs only PID + generation, not another WMI table.
// CIM truncates Windows FILETIME to microseconds; keep its existing
// marker format so both sources identify the same process generation.
return [
"$ErrorActionPreference='Stop'", 'trap { exit 1 }', '$rows=@(',
`foreach($processId in @(${pids.join(',')})){`,
' $p=$null',
' try { $p=[System.Diagnostics.Process]::GetProcessById($processId) }',
' catch [System.ArgumentException] { continue }',
' try {',
' try { $marker=$p.StartTime.ToUniversalTime().ToString("yyyy-MM-dd\'T\'HH:mm:ss.ffffff\'0Z\'", [System.Globalization.CultureInfo]::InvariantCulture) }',
' catch { if($p.HasExited){continue}; throw }',
' [pscustomobject]@{ProcessId=$processId;ParentProcessId=0;CreationDate=$marker}',
' } finally { if($p){$p.Dispose()} }',
'}', ')', '@{complete=$true;processes=$rows} | ConvertTo-Json -Depth 4 -Compress',
].join('\n')
}
const properties = `ProcessId,ParentProcessId,CreationDate${includeCommandLine ? ',Name,CommandLine' : ''}`
const filter = pids?.length ? ` -Filter "${pids.map(pid => `ProcessId = ${pid}`).join(' OR ')}"` : ''
return [
"$ErrorActionPreference='Stop'",
'trap { exit 1 }',
`$rows=@(Get-CimInstance Win32_Process -Property ${properties}${filter} | Select-Object ProcessId,ParentProcessId,Name,CommandLine,`,
" @{N='CreationDate';E={if ($_.CreationDate) { $_.CreationDate.ToUniversalTime().ToString('o') } else { $null }}})",
'@{complete=$true;processes=$rows} | ConvertTo-Json -Depth 4 -Compress',
].join('\n')
}
export const WINDOWS_PROCESS_TABLE_COMMAND = windowsProcessTableCommand()
function execute(file: string, args: string[], timeout: number): Promise<string | null> {
if (timeout <= 0) return Promise.resolve(null)
return new Promise(resolve => {
execFile(file, args, {
encoding: 'utf8', windowsHide: true, timeout: Math.ceil(timeout),
killSignal: 'SIGKILL', maxBuffer: 16 * 1024 * 1024,
}, (error, stdout) => resolve(error ? null : stdout))
})
}
export async function readWindowsProcessTable(timeout = 15_000, pids?: number[], includeCommandLine = false): Promise<WindowsProcessTable | null> {
if (pids?.some(pid => !Number.isInteger(pid) || pid < 0)) return null
const output = await execute('powershell', ['-NoProfile', '-NonInteractive', '-Command', windowsProcessTableCommand(pids, includeCommandLine)], timeout)
if (output === null) return null
try {
const parsed = JSON.parse(output)
if (parsed?.complete !== true || !Array.isArray(parsed.processes) || (!pids?.length && !parsed.processes.length)) return null
const table: WindowsProcessTable = new Map()
for (const entry of parsed.processes) {
if (!entry || !Number.isInteger(entry.ProcessId) || entry.ProcessId < 0
|| !Number.isInteger(entry.ParentProcessId) || entry.ParentProcessId < 0
|| (entry.CreationDate !== null && typeof entry.CreationDate !== 'string')
|| table.has(entry.ProcessId)) return null
table.set(entry.ProcessId, { parentPid: entry.ParentProcessId, marker: entry.CreationDate || null,
name: typeof entry.Name === 'string' ? entry.Name : '',
commandLine: typeof entry.CommandLine === 'string' ? entry.CommandLine : '',
})
}
return table
} catch { return null }
}
/** A failed probe conservatively keeps ownership; it must never authorize a kill. */
export function windowsGenerationMayBeAlive(table: WindowsProcessTable | null, pid: number, marker?: string): boolean {
if (table === null) return true
const entry = table.get(pid)
return Boolean(entry && (!entry.marker || !marker || entry.marker === marker))
}
export type WindowsStopReport = { confirmed: boolean; phase: string; elapsedMs: number; snapshots: number }
export async function stopWindowsProcessTree(rootPid: number, force: boolean, report?: (result: WindowsStopReport) => void): Promise<boolean> {
// Includes command startup, queries, signalling and verification, independent
// of tree size. Each async child is killed when its remaining budget expires.
const started = performance.now()
const deadline = started + 45_000
let phase = 'initial-snapshot', snapshots = 1
const finish = (confirmed: boolean) => {
report?.({ confirmed, phase, elapsedMs: Math.round(performance.now() - started), snapshots })
return confirmed
}
const remaining = () => Math.max(0, deadline - performance.now())
const signal = async (pid: number, hard: boolean, tree: boolean) => {
phase = 'signal'
return execute('taskkill', [
...(hard ? ['/F'] : []), ...(tree ? ['/T'] : []), '/PID', String(pid),
], Math.min(5_000, remaining()))
}
const initial = await readWindowsProcessTable(Math.min(15_000, remaining()))
if (!initial) {
// A transient CIM failure must not leave the cancellation path with no
// termination attempt. A direct process probe still supplies a
// generation-safe witness for the known root; descendants remain
// unverified, so the result stays unconfirmed.
phase = 'root-fallback-probe'; snapshots++
const rootProbe = await readWindowsProcessTable(Math.min(5_000, remaining()), [rootPid])
if (rootProbe?.get(rootPid)) await signal(rootPid, force, true)
return finish(false)
}
const members = new Map<number, string | null>()
const queue = [rootPid]
while (queue.length) {
const pid = queue.shift()!
if (members.has(pid)) continue
members.set(pid, initial.get(pid)?.marker ?? null)
for (const [child, entry] of initial) if (child !== pid && entry.parentPid === pid) queue.push(child)
}
const unresolved = new Map(members)
const snapshot = () => {
phase = 'verification-snapshot'; snapshots++
return readWindowsProcessTable(Math.min(10_000, remaining()), [...unresolved.keys()])
}
const refresh = (table: WindowsProcessTable) => {
for (const [pid, marker] of unresolved) {
const current = table.get(pid)
// A different known generation proves the ORIGINAL process exited.
// Once resolved, PID reuse cannot reintroduce it into this tree.
if (!current || (marker && current.marker && marker !== current.marker)) unresolved.delete(pid)
}
}
refresh(initial)
if (!unresolved.size) return finish(true)
// Recheck the root's generation immediately before tree-wide signalling.
let current = await snapshot()
if (!current) return finish(false)
refresh(current)
const rootMarker = members.get(rootPid)
if (unresolved.has(rootPid) && rootMarker && current.get(rootPid)?.marker === rootMarker) {
const softAccepted = await signal(rootPid, force, true)
if (!force && softAccepted !== null) {
const graceDeadline = Math.min(deadline, performance.now() + 2_000)
while (unresolved.size && performance.now() < graceDeadline) {
current = await snapshot()
if (!current) return finish(false)
refresh(current)
if (unresolved.size && performance.now() < graceDeadline) {
await new Promise(resolve => setTimeout(resolve, Math.min(100, graceDeadline - performance.now())))
}
}
}
}
if (!force && remaining()) {
// A console tree can grow while soft termination is refused or drains.
// Capture those descendants before escalation, rather than killing only
// the original PIDs and mistaking their exit for complete cleanup.
phase = 'escalation-snapshot'; snapshots++
const expanded = await readWindowsProcessTable(Math.min(15_000, remaining()))
if (!expanded) return finish(false)
refresh(expanded)
const root = expanded.get(rootPid)
if (!root || (rootMarker && root.marker === rootMarker)) {
const pending = [rootPid], seen = new Set<number>()
while (pending.length) {
const pid = pending.shift()!
if (seen.has(pid)) continue
seen.add(pid)
const entry = expanded.get(pid)
if (entry && !members.has(pid)) {
members.set(pid, entry.marker)
unresolved.set(pid, entry.marker)
}
for (const [child, candidate] of expanded) if (child !== pid && candidate.parentPid === pid) pending.push(child)
}
}
// Never tree-kill a replacement root. Recheck once more after the full
// snapshot so a slow CIM scan cannot authorize signalling a reused PID.
if (unresolved.has(rootPid) && rootMarker && root?.marker === rootMarker) {
current = await snapshot()
if (!current) return finish(false)
refresh(current)
if (unresolved.has(rootPid) && current.get(rootPid)?.marker === rootMarker) {
await signal(rootPid, true, true)
}
}
}
// A missing intermediate process disconnects a survivor from the root's
// taskkill /T traversal. Expand each surviving generation's OWN subtree,
// including new children, before forcing that subtree and verifying it.
const pendingSignals = [...unresolved.keys()].reverse()
const scheduled = new Set(pendingSignals)
for (let index = 0; index < pendingSignals.length; index++) {
const pid = pendingSignals[index]
if (!unresolved.size) return finish(true)
if (!remaining()) return finish(false)
current = await snapshot()
if (!current) return finish(false)
refresh(current)
if (!unresolved.size) return finish(true)
const expected = unresolved.get(pid)
if (!expected || current.get(pid)?.marker !== expected) continue
phase = 'survivor-subtree-snapshot'; snapshots++
const subtree = await readWindowsProcessTable(Math.min(15_000, remaining()))
if (!subtree) return finish(false)
refresh(subtree)
if (!unresolved.has(pid) || subtree.get(pid)?.marker !== expected) continue
const descendants = [pid], seen = new Set<number>()
while (descendants.length) {
const child = descendants.shift()!
if (seen.has(child)) continue
seen.add(child)
const entry = subtree.get(child)
if (!entry) continue
const captured = members.get(child)
if (captured && entry.marker && captured !== entry.marker) continue
if (!members.has(child)) {
members.set(child, entry.marker)
unresolved.set(child, entry.marker)
}
for (const [next, candidate] of subtree) if (next !== child && candidate.parentPid === child) descendants.push(next)
}
current = await snapshot()
if (!current) return finish(false)
refresh(current)
if (unresolved.has(pid) && current.get(pid)?.marker === expected) await signal(pid, true, true)
for (const child of unresolved.keys()) {
if (!scheduled.has(child)) { scheduled.add(child); pendingSignals.push(child) }
}
}
while (remaining()) {
current = await snapshot()
if (!current) return finish(false)
refresh(current)
if (!unresolved.size) return finish(true)
await new Promise(resolve => setTimeout(resolve, Math.min(100, remaining())))
}
return finish(false)
}