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>
This commit is contained in:
18
cmd/bd/daemon_lock_unix.go
Normal file
18
cmd/bd/daemon_lock_unix.go
Normal file
@@ -0,0 +1,18 @@
|
||||
//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
|
||||
}
|
||||
Reference in New Issue
Block a user