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:
Steve Yegge
2025-10-22 13:59:21 -07:00
parent d1d3fcdf02
commit e291f464ee
9 changed files with 475 additions and 76 deletions

View File

@@ -244,21 +244,8 @@ func getLogFilePath(userPath string, global bool) (string, error) {
}
func isDaemonRunning(pidFile string) (bool, int) {
data, err := os.ReadFile(pidFile)
if err != nil {
return false, 0
}
pid, err := strconv.Atoi(strings.TrimSpace(string(data)))
if err != nil {
return false, 0
}
if !isProcessRunning(pid) {
return false, 0
}
return true, pid
beadsDir := filepath.Dir(pidFile)
return tryDaemonLock(beadsDir)
}
func formatUptime(seconds float64) string {
@@ -768,6 +755,19 @@ func runDaemonLoop(interval time.Duration, autoCommit, autoPush bool, logPath, p
fmt.Fprintf(logF, "[%s] %s\n", timestamp, msg)
}
// Acquire daemon lock FIRST - this is the single source of truth for exclusivity
beadsDir := filepath.Dir(pidFile)
lock, err := acquireDaemonLock(beadsDir, global)
if err != nil {
if err == ErrDaemonLocked {
log("Daemon already running (lock held), exiting")
os.Exit(1)
}
log("Error acquiring daemon lock: %v", err)
os.Exit(1)
}
defer lock.Close()
myPID := os.Getpid()
pidFileCreated := false