fix(gsd): only unlink notification lock when owned, prevent foreign lock deletion

_withLock() was unconditionally unlinking the lock file in finally,
even when lock acquisition failed. This could delete another process's
lock and allow unlocked concurrent writes. Now tracks ownership and
only cleans up locks we created.
This commit is contained in:
Jeremy 2026-04-06 17:39:44 -05:00
parent 2c4ac844f1
commit d553455732

View file

@ -275,14 +275,19 @@ function _withLock<T>(basePath: string, fn: () => T): T {
}
}
// Only run the mutation if we actually own the lock
const ownsLock = fd !== null;
try {
// Write our PID timestamp into the lock for stale detection
if (fd !== null) {
if (ownsLock && fd !== null) {
// Write our PID timestamp into the lock for stale detection
writeFileSync(lockPath, String(Date.now()), "utf-8");
closeSync(fd);
}
return fn();
} finally {
try { unlinkSync(lockPath); } catch { /* best-effort cleanup */ }
// Only delete the lock if we created it — never remove another process's lock
if (ownsLock) {
try { unlinkSync(lockPath); } catch { /* best-effort cleanup */ }
}
}
}