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

@@ -0,0 +1,45 @@
package main
import (
"os/exec"
"path/filepath"
"testing"
)
// TestDaemonExitsWhenParentDies verifies that the daemon exits when its parent process dies
func TestDaemonExitsWhenParentDies(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
t.Skip("Manual test - requires daemon to be run externally")
// This is a manual test scenario:
// 1. Start a shell process that spawns the daemon
// 2. Verify daemon tracks parent PID
// 3. Kill the shell process
// 4. Verify daemon exits within 10-15 seconds
//
// To test manually:
// $ sh -c 'bd daemon --interval 5s & sleep 100' &
// $ SHELL_PID=$!
// $ # Check daemon.lock has parent_pid set to SHELL_PID
// $ kill $SHELL_PID
// $ # Daemon should exit within 10-15 seconds
}
func mustAbs(t *testing.T, path string) string {
abs, err := filepath.Abs(path)
if err != nil {
t.Fatalf("Failed to get absolute path: %v", err)
}
return abs
}
func runGitCmd(t *testing.T, dir string, args ...string) {
cmd := exec.Command("git", args...)
cmd.Dir = dir
if err := cmd.Run(); err != nil {
t.Fatalf("git %v failed: %v", args, err)
}
}