Files
beads/cmd/bd/daemon_lock_unix.go
Steve Yegge e291f464ee Fix bd-54: Prevent multiple daemon instances via file locking
- Implemented daemon.lock using flock (Unix) and LockFileEx (Windows)
- Lock acquired before PID file, held for daemon lifetime
- Eliminates race conditions in concurrent daemon starts
- Backward compatible: falls back to PID check for old daemons
- Updated isDaemonRunning() to check lock availability
- All tests pass including new lock and backward compatibility tests

Amp-Thread-ID: https://ampcode.com/threads/T-0e2627f4-03f9-4024-bb4b-21d23d296300
Co-authored-by: Amp <amp@ampcode.com>
2025-10-22 13:59:58 -07:00

19 lines
316 B
Go

//go:build unix
package main
import (
"os"
"golang.org/x/sys/unix"
)
// flockExclusive acquires an exclusive non-blocking lock on the file
func flockExclusive(f *os.File) error {
err := unix.Flock(int(f.Fd()), unix.LOCK_EX|unix.LOCK_NB)
if err == unix.EWOULDBLOCK {
return ErrDaemonLocked
}
return err
}