refactor(shared): reuse isPidAlive

This commit is contained in:
Peter Steinberger
2026-02-15 19:06:47 +00:00
parent c682634188
commit 5248b759fe
4 changed files with 17 additions and 39 deletions

View File

@@ -1,6 +1,7 @@
import fsSync from "node:fs";
import fs from "node:fs/promises";
import path from "node:path";
import { isPidAlive } from "../shared/pid-alive.js";
type LockFilePayload = {
pid: number;
@@ -48,18 +49,6 @@ function resolveCleanupState(): CleanupState {
return proc[CLEANUP_STATE_KEY];
}
function isAlive(pid: number): boolean {
if (!Number.isFinite(pid) || pid <= 0) {
return false;
}
try {
process.kill(pid, 0);
return true;
} catch {
return false;
}
}
/**
* Synchronously release all held locks.
* Used during process exit when async operations aren't reliable.
@@ -206,7 +195,7 @@ export async function acquireSessionWriteLock(params: {
const payload = await readLockPayload(lockPath);
const createdAt = payload?.createdAt ? Date.parse(payload.createdAt) : NaN;
const stale = !Number.isFinite(createdAt) || Date.now() - createdAt > staleMs;
const alive = payload?.pid ? isAlive(payload.pid) : false;
const alive = payload?.pid ? isPidAlive(payload.pid) : false;
if (stale || !alive) {
await fs.rm(lockPath, { force: true });
continue;