fix(linear): keep lock file stable across Release cycles

Removing the lock file after unlocking creates a lifecycle race: a blocked
waiter acquires the old inode (between flock release and os.Remove), then a
third process creates a new file at the same path and locks it — both
processes believe they hold the lock.

Fix: never call os.Remove in Release(). Truncate diagnostic metadata while
still holding the flock, then release. The stable path is reused by
subsequent AcquireSyncLock calls via O_CREATE|O_RDWR.

Also:
- Handle writeLockInfo's Truncate error (was silently discarded with _ =)
- Add #nosec G304 to readLockInfo (path is program-constructed, not user input)
- Update test: assert lock file persists and is empty after Release, not removed
This commit is contained in:
kev 2026-05-02 18:55:08 -04:00
commit 175bc23d1f
2 changed files with 40 additions and 16 deletions

View file

@ -67,23 +67,35 @@ func AcquireSyncLock(beadsDir string, wait bool) (*SyncLock, error) {
return &SyncLock{path: lockPath, file: f}, nil
}
// Release releases the sync lock and removes the lock file.
// Release releases the sync lock. The lock file is NOT removed — removing it
// after unlocking creates a race where a blocked waiter acquires the old inode
// while a new process creates a fresh file at the same path, splitting lock
// identity. Instead the file is truncated while still holding the flock, then
// the flock is released. The stable path is reused by subsequent Acquire calls.
func (l *SyncLock) Release() error {
if l == nil || l.file == nil {
return nil
}
// Truncate the file before unlocking so other processes don't read stale PID info
_ = l.file.Truncate(0)
err := lockfile.FlockUnlock(l.file)
closeErr := l.file.Close()
_ = os.Remove(l.path)
l.file = nil
if err != nil {
return err
// Clear diagnostic metadata while still holding the flock so no reader
// observes a stale PID after we unlock. Truncate failure is non-fatal
// (metadata-only), but we capture it to surface if unlock/close succeed.
var truncErr error
if err := l.file.Truncate(0); err != nil {
truncErr = fmt.Errorf("clearing lock metadata: %w", err)
}
return closeErr
unlockErr := lockfile.FlockUnlock(l.file)
closeErr := l.file.Close()
l.file = nil
if unlockErr != nil {
return unlockErr
}
if closeErr != nil {
return closeErr
}
return truncErr
}
// SyncLockHeldError is returned when the lock is held by another process.
@ -100,18 +112,19 @@ func (e *SyncLockHeldError) Error() string {
}
func writeLockInfo(f *os.File) error {
_ = f.Truncate(0)
_, err := f.Seek(0, 0)
if err != nil {
if err := f.Truncate(0); err != nil {
return fmt.Errorf("truncating lock file: %w", err)
}
if _, err := f.Seek(0, 0); err != nil {
return err
}
info := fmt.Sprintf("pid=%d\nstarted=%s\n", os.Getpid(), time.Now().UTC().Format(time.RFC3339))
_, err = f.WriteString(info)
_, err := f.WriteString(info)
return err
}
func readLockInfo(path string) *SyncLockInfo {
data, err := os.ReadFile(path)
data, err := os.ReadFile(path) // #nosec G304 -- path is constructed from beadsDir, not user input
if err != nil {
return nil
}

View file

@ -26,8 +26,19 @@ func TestAcquireSyncLock(t *testing.T) {
t.Fatalf("failed to release lock: %v", err)
}
if _, err := os.Stat(lockPath); !os.IsNotExist(err) {
t.Error("lock file should be removed after release")
// Lock file must persist after release — removing it after unlock creates a
// race where a blocked waiter holds the old inode while a new process opens a
// fresh file at the same path, splitting lock identity.
if _, err := os.Stat(lockPath); err != nil {
t.Errorf("lock file should persist after release (stable path): %v", err)
}
// Content should be truncated (empty) after release.
data, err := os.ReadFile(lockPath)
if err != nil {
t.Fatalf("could not read lock file after release: %v", err)
}
if len(data) != 0 {
t.Errorf("lock file should be empty after release, got %q", string(data))
}
})