fix: harden startup lock handling (#1306)

This commit is contained in:
harshitsinghbhandari 2026-04-19 00:32:50 +05:30
parent c6129de1c9
commit 0481dbf158
1 changed files with 11 additions and 7 deletions

View File

@ -37,7 +37,10 @@ function isProcessAlive(pid: number): boolean {
try {
process.kill(pid, 0);
return true;
} catch {
} catch (error: unknown) {
if ((error as { code?: string }).code === "EPERM") {
return true;
}
return false;
}
}
@ -94,13 +97,14 @@ async function acquireLock(
const release = tryAcquire(lockFile);
if (release) return release;
const owner = readLockMetadata(lockFile);
if (!owner || !isProcessAlive(owner.pid)) {
try { unlinkSync(lockFile); } catch { /* ignore */ }
const retryRelease = tryAcquire(lockFile);
if (retryRelease) return retryRelease;
}
if (Date.now() - start > timeoutMs) {
const owner = readLockMetadata(lockFile);
if (!owner || !isProcessAlive(owner.pid)) {
try { unlinkSync(lockFile); } catch { /* ignore */ }
const finalRelease = tryAcquire(lockFile);
if (finalRelease) return finalRelease;
}
throw new Error(`Could not acquire ${resourceName}`);
}