Fix broker PID persistence and orphaned process detection#523
Fix broker PID persistence and orphaned process detection#523khaliqgant merged 2 commits intomainfrom
Conversation
Change grep -e to grep -F when matching broker names in killOrphanedBrokerProcesses to avoid regex metacharacter issues in project directory names containing dots or other special chars. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
| relay = started.relay; | ||
| apiPort = started.apiPort; | ||
| writeBrokerPid(brokerPidPath, deps.pid, deps); | ||
| writeBrokerPid(brokerPidPath, relay.brokerPid ?? deps.pid, deps); |
There was a problem hiding this comment.
🔴 Writing broker child PID instead of parent PID causes agent-relay down to orphan the parent CLI process and dashboard
The change from deps.pid to relay.brokerPid ?? deps.pid writes the broker child process PID to the PID file. When agent-relay down reads this PID and sends SIGTERM (src/cli/lib/broker-lifecycle.ts:1103), it kills the broker child directly. However, the parent agent-relay up process is still blocked on holdOpen() (src/cli/lib/broker-lifecycle.ts:997), which is a promise that never resolves (src/cli/commands/core.ts:339). The parent's SIGTERM/SIGINT signal handlers (src/cli/lib/broker-lifecycle.ts:976-995) are never triggered, so shutdownOnce() is never called, meaning the dashboard process (dashboardProcess) is never killed (src/cli/lib/broker-lifecycle.ts:701-706) and the parent process itself is never exited. This leaves both the parent CLI process and the dashboard as orphans after agent-relay down completes.
Affected scenario: background mode
In --background mode, the parent is a detached process (src/cli/lib/broker-lifecycle.ts:717-729). After agent-relay down kills only the broker grandchild, the detached parent becomes an invisible orphan that the user cannot easily discover or stop.
| writeBrokerPid(brokerPidPath, relay.brokerPid ?? deps.pid, deps); | |
| writeBrokerPid(brokerPidPath, deps.pid, deps); |
Was this helpful? React with 👍 or 👎 to provide feedback.
Summary
relay.brokerPid) instead of the Node.js CLI PID (deps.pid). When the CLI exits but the persist-mode broker survives, the PID file now correctly identifies the running broker, preventing false "stale PID" detection on nextagent-relay up.killOrphanedBrokerProcessesnow greps by broker name (--name relay) instead of only by project root path. The installed binary (~/.agent-relay/bin/agent-relay-broker) doesn't contain the project root in its command line, so the old grep never matched orphaned brokers. Falls back to path-based grep for dev builds.grep -etogrep -Ffor broker name matching to prevent regex metacharacter issues in project names containing dots or special chars.Test plan
core.test.tstests passagent-relay up, kill the Node.js CLI (not the broker), runagent-relay upagain — should detect running broker correctlyagent-relay down --forcefollowed byagent-relay up— should start cleanly🤖 Generated with Claude Code