mirror of
https://github.com/wu736139669/hapi.git
synced 2026-12-05 07:07:04 +00:00
fix: detect stale PID in runner state after abnormal shutdown (#931)
* fix: verify PID belongs to hapi before treating runner as alive After OS upgrade, stale runner.state.json PID can be reused by unrelated processes. The old kill(pid, 0) check passes for any process, causing start-sync to loop with 'Runner already running' indefinitely. Now uses ps/wmic to confirm the process command line contains 'hapi' before considering the runner alive. Falls back to alive-only check if ps/wmic fails. * fix: precise runner process detection and wmic fallback * fix: add fallback for ps failure in non-Windows branch
This commit is contained in:
@@ -16,6 +16,32 @@ export function isProcessAlive(pid: number): boolean {
|
||||
}
|
||||
}
|
||||
|
||||
// ponytail: ps -p is cheap and avoids PID-reuse false positives after OS upgrades/reboots
|
||||
function isRunnerCommand(commandLine: string): boolean {
|
||||
return /(?:^|\s)runner(?:\s|$)/.test(commandLine) && /(?:^|\s)start-sync(?:\s|$)/.test(commandLine);
|
||||
}
|
||||
|
||||
export function isHapiRunnerProcess(pid: number): boolean {
|
||||
if (!isProcessAlive(pid)) {
|
||||
return false;
|
||||
}
|
||||
if (isWindows()) {
|
||||
const result = spawn.sync('wmic', ['process', 'where', `ProcessId=${pid}`, 'get', 'CommandLine'], { stdio: 'pipe' });
|
||||
if (result.error) {
|
||||
return true;
|
||||
}
|
||||
if (result.status !== 0) {
|
||||
return isProcessAlive(pid);
|
||||
}
|
||||
return isRunnerCommand(result.stdout?.toString() ?? '');
|
||||
}
|
||||
const result = spawn.sync('ps', ['-p', String(pid), '-o', 'command='], { stdio: 'pipe' });
|
||||
if (result.error || result.status !== 0) {
|
||||
return isProcessAlive(pid);
|
||||
}
|
||||
return isRunnerCommand(result.stdout?.toString() ?? '');
|
||||
}
|
||||
|
||||
function killProcessWindows(pid: number, force: boolean): boolean {
|
||||
if (!isProcessAlive(pid)) {
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user