Skip to content

Commit 264d718

Browse files
baudbot-agentBaudbot
andauthored
Fix: Aggressively kill all bridge processes on startup (#179)
Co-authored-by: Baudbot <hornet@agentmail.to>
1 parent f60ad4f commit 264d718

1 file changed

Lines changed: 22 additions & 11 deletions

File tree

pi/skills/control-agent/startup-pi.sh

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,26 +98,37 @@ echo "Cleaning up old bridge..."
9898
# the bridge while we're trying to clean up the port.
9999
tmux kill-session -t "$BRIDGE_TMUX_SESSION" 2>/dev/null || true
100100

101-
# Now gracefully stop any process on the port. SIGTERM lets the bridge close
102-
# the HTTP server and release the port cleanly; SIGKILL is the fallback.
103-
PORT_PIDS=$(lsof -ti :7890 2>/dev/null || true)
104-
if [ -n "$PORT_PIDS" ]; then
105-
echo "Stopping processes on port 7890 (SIGTERM): $PORT_PIDS"
106-
echo "$PORT_PIDS" | xargs kill 2>/dev/null || true
101+
# Kill ALL bridge processes (broker-bridge.mjs and bridge.mjs) to prevent
102+
# orphaned processes from holding port 7890 after control-agent restarts.
103+
# This is more aggressive than just killing port holders, but prevents the
104+
# common failure mode where a bridge process survives tmux session cleanup
105+
# (e.g., detached, zombied, or in a different session tree).
106+
BRIDGE_PIDS=$(pgrep -f 'node (broker-)?bridge\.mjs' 2>/dev/null || true)
107+
if [ -n "$BRIDGE_PIDS" ]; then
108+
echo "Killing all bridge processes (SIGTERM): $BRIDGE_PIDS"
109+
echo "$BRIDGE_PIDS" | xargs kill 2>/dev/null || true
107110
# Wait up to 3s for graceful shutdown
108111
for i in 1 2 3; do
109112
sleep 1
110-
PORT_PIDS=$(lsof -ti :7890 2>/dev/null || true)
111-
[ -z "$PORT_PIDS" ] && break
113+
BRIDGE_PIDS=$(pgrep -f 'node (broker-)?bridge\.mjs' 2>/dev/null || true)
114+
[ -z "$BRIDGE_PIDS" ] && break
112115
done
113116
# Force-kill anything that didn't exit
114-
if [ -n "$PORT_PIDS" ]; then
115-
echo "Force-killing stubborn processes: $PORT_PIDS"
116-
echo "$PORT_PIDS" | xargs kill -9 2>/dev/null || true
117+
if [ -n "$BRIDGE_PIDS" ]; then
118+
echo "Force-killing stubborn bridge processes: $BRIDGE_PIDS"
119+
echo "$BRIDGE_PIDS" | xargs kill -9 2>/dev/null || true
117120
sleep 1
118121
fi
119122
fi
120123

124+
# Final safety check: kill anything still on port 7890
125+
PORT_PIDS=$(lsof -ti :7890 2>/dev/null || true)
126+
if [ -n "$PORT_PIDS" ]; then
127+
echo "Force-killing remaining processes on port 7890: $PORT_PIDS"
128+
echo "$PORT_PIDS" | xargs kill -9 2>/dev/null || true
129+
sleep 1
130+
fi
131+
121132
OLD_PID_FILE="$HOME/.pi/agent/slack-bridge.pid"
122133
if [ -f "$OLD_PID_FILE" ]; then
123134
OLD_PID="$(cat "$OLD_PID_FILE" 2>/dev/null || true)"

0 commit comments

Comments
 (0)