Fix daemon orphaning: track parent PID and exit when parent dies

- Add ParentPID field to DaemonLockInfo struct
- Daemon monitors parent process every 10 seconds
- Gracefully exits when parent process dies
- Prevents accumulation of orphaned daemons from dead sessions
- Fixes race conditions from multiple daemons on same database

Closes bd-zpnq
This commit is contained in:
Steve Yegge
2025-11-07 18:57:43 -08:00
parent a6c9579645
commit 7fa0c93195
5 changed files with 105 additions and 5 deletions

View File

@@ -16,6 +16,7 @@ var ErrDaemonLocked = errors.New("daemon lock already held by another process")
// DaemonLockInfo represents the metadata stored in the daemon.lock file
type DaemonLockInfo struct {
PID int `json:"pid"`
ParentPID int `json:"parent_pid,omitempty"` // Parent process ID (0 if not tracked)
Database string `json:"database"`
Version string `json:"version"`
StartedAt time.Time `json:"started_at"`
@@ -63,6 +64,7 @@ func acquireDaemonLock(beadsDir string, dbPath string) (*DaemonLock, error) {
// Write JSON metadata to the lock file
lockInfo := DaemonLockInfo{
PID: os.Getpid(),
ParentPID: os.Getppid(),
Database: dbPath,
Version: Version,
StartedAt: time.Now().UTC(),