From 68f9bef98c4c3546bde49bb54e5d85f6f1e8c1af Mon Sep 17 00:00:00 2001 From: "Charles P. Cross" Date: Tue, 18 Nov 2025 18:36:33 -0500 Subject: [PATCH] fix: prevent daemon from exiting when launcher process exits (issue #278) --- cmd/bd/daemon.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/cmd/bd/daemon.go b/cmd/bd/daemon.go index 6c67d14d..f4c3c20d 100644 --- a/cmd/bd/daemon.go +++ b/cmd/bd/daemon.go @@ -179,6 +179,19 @@ func init() { daemonCmd.Flags().BoolVar(&jsonOutput, "json", false, "Output JSON format") rootCmd.AddCommand(daemonCmd) } + +// computeDaemonParentPID determines which parent PID the daemon should track. +// When BD_DAEMON_FOREGROUND=1 (used by startDaemon for background CLI launches), +// we return 0 to disable parent tracking, since the short-lived launcher +// process is expected to exit immediately after spawning the daemon. +// In all other cases we track the current OS parent PID. +func computeDaemonParentPID() int { + if os.Getenv("BD_DAEMON_FOREGROUND") == "1" { + // 0 means "not tracked" in checkParentProcessAlive + return 0 + } + return os.Getppid() +} func runDaemonLoop(interval time.Duration, autoCommit, autoPush bool, logPath, pidFile string, global bool) { logF, log := setupDaemonLogger(logPath) defer func() { _ = logF.Close() }() @@ -422,7 +435,7 @@ func runDaemonLoop(interval time.Duration, autoCommit, autoPush bool, logPath, p doSync() // Get parent PID for monitoring (exit if parent dies) - parentPID := os.Getppid() + parentPID := computeDaemonParentPID() log.log("Monitoring parent process (PID %d)", parentPID) // Choose event loop based on BEADS_DAEMON_MODE