Skip to content

Fix broker PID persistence and orphaned process detection#523

Merged
khaliqgant merged 2 commits intomainfrom
fix/relay-broker-pid-matching
Mar 10, 2026
Merged

Fix broker PID persistence and orphaned process detection#523
khaliqgant merged 2 commits intomainfrom
fix/relay-broker-pid-matching

Conversation

@khaliqgant
Copy link
Copy Markdown
Member

@khaliqgant khaliqgant commented Mar 10, 2026

Summary

  • Write correct broker PID: Changed PID file to store the Rust broker process PID (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 next agent-relay up.
  • Fix orphaned broker detection: killOrphanedBrokerProcesses now 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.
  • Use fixed-string grep: Changed grep -e to grep -F for broker name matching to prevent regex metacharacter issues in project names containing dots or special chars.

Test plan

  • All 22 existing core.test.ts tests pass
  • Manual test: agent-relay up, kill the Node.js CLI (not the broker), run agent-relay up again — should detect running broker correctly
  • Manual test: agent-relay down --force followed by agent-relay up — should start cleanly
  • Verify PID file contains the Rust broker PID (not the Node.js PID) after startup

🤖 Generated with Claude Code


Open with Devin

khaliqgant and others added 2 commits March 10, 2026 10:07
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>
@khaliqgant khaliqgant merged commit 75ee8c2 into main Mar 10, 2026
37 of 38 checks passed
@khaliqgant khaliqgant deleted the fix/relay-broker-pid-matching branch March 10, 2026 09:21
Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

View 3 additional findings in Devin Review.

Open in Devin Review

relay = started.relay;
apiPort = started.apiPort;
writeBrokerPid(brokerPidPath, deps.pid, deps);
writeBrokerPid(brokerPidPath, relay.brokerPid ?? deps.pid, deps);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 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.

Suggested change
writeBrokerPid(brokerPidPath, relay.brokerPid ?? deps.pid, deps);
writeBrokerPid(brokerPidPath, deps.pid, deps);
Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant