Multiple MCP clients on one host kill each other's bridge: make config path configurable (+ --help must not take the lock) #23

Open
opened 2026-11-21 18:42:30 +00:00 by jmz · 0 comments
Owner

Problem

Several Claude Code sessions on the same host (rpi5) each start matrix-bridge-mcp as their matrix MCP server. They all share one Matrix device and store, so they keep knocking each other offline.

  • One fixed config: every instance reads the hard-coded ~/.matrix-bridge/config.json (src/config.rs:84, config_path()). That means one store_path (~/.matrix-bridge/store) and one device (rpi5-0o3kuj3y).
  • Takeover on every start: the single-instance lock (src/mcp/single_instance.rs) lets a newcomer take over .bridge.lock and SIGTERM/SIGKILL the holder (single-instance: superseding stale bridge pid … (-TERM)).
  • Result: every /mcp reconnect in one session kills Matrix in the other. Seen repeatedly on 2026-11-21 with two parallel sessions.
  • --help starts the bridge: at 18:33:50 UTC, /usr/local/bin/matrix-bridge-mcp --help didn't print help. It acquired the lock, superseded the other session's bridge (pid 1102367), then panicked on stdin close (JoinError::Cancelled in deadpool-runtime, then connection closed: initialize request). The installed binary may predate the clap CLI in src/main.rs; not checked.

Workaround in place

One session owns the bridge. The others relay posts to it via Claude Code cross-session messages, and their matrix server is turned off in /mcp.

Proposed fix

  1. Configurable config path: a --config <path> flag and a MATRIX_BRIDGE_CONFIG env var, defaulting to ~/.matrix-bridge/config.json.
    • Each session/MCP entry can then point at its own store_path and device_name, without a HOME= wrapper hack.
  2. Lock keyed by store, not by host: the lock lives in the store directory, so this already works once (1) exists. Document that instances must not share a store.
  3. --help / --version exit before taking the lock: a help call must never supersede a running bridge.
  4. Optional, bigger: a shared daemon mode. One long-lived bridge process holds the store/device, and each MCP client attaches to it over a local socket instead of starting its own bridge. This avoids one device per session, along with the extra logins and cross-signing that come with it.
  5. Takeover policy: a flag (e.g. --no-takeover) under which a newcomer exits with a clear error instead of killing the holder. This is useful for a second, accidental start.

Per-device cost if (1) is used without (4)

Each additional device needs its own login (credentials.json) and cross-signing via the SSSS recovery key (MATRIX_BRIDGE_RECOVERY_KEY, f2c643e). Otherwise peers see an unverified device under trust_mode: tofu.

🤖 Generated with Claude Code

https://claude.ai/code/session_01XMf4EH67dMry1FyMPoDPQ1

## Problem Several Claude Code sessions on the same host (rpi5) each start `matrix-bridge-mcp` as their `matrix` MCP server. They all share one Matrix device and store, so they keep knocking each other offline. - **One fixed config:** every instance reads the hard-coded `~/.matrix-bridge/config.json` (`src/config.rs:84`, `config_path()`). That means one `store_path` (`~/.matrix-bridge/store`) and one device (`rpi5-0o3kuj3y`). - **Takeover on every start:** the single-instance lock (`src/mcp/single_instance.rs`) lets a newcomer take over `.bridge.lock` and SIGTERM/SIGKILL the holder (`single-instance: superseding stale bridge pid … (-TERM)`). - **Result:** every `/mcp` reconnect in one session kills Matrix in the other. Seen repeatedly on 2026-11-21 with two parallel sessions. - **`--help` starts the bridge:** at 18:33:50 UTC, `/usr/local/bin/matrix-bridge-mcp --help` didn't print help. It acquired the lock, superseded the other session's bridge (pid 1102367), then panicked on stdin close (`JoinError::Cancelled` in deadpool-runtime, then `connection closed: initialize request`). The installed binary may predate the clap CLI in `src/main.rs`; not checked. ## Workaround in place One session owns the bridge. The others relay posts to it via Claude Code cross-session messages, and their `matrix` server is turned off in `/mcp`. ## Proposed fix 1. **Configurable config path:** a `--config <path>` flag and a `MATRIX_BRIDGE_CONFIG` env var, defaulting to `~/.matrix-bridge/config.json`. - Each session/MCP entry can then point at its own `store_path` and `device_name`, without a `HOME=` wrapper hack. 2. **Lock keyed by store, not by host:** the lock lives in the store directory, so this already works once (1) exists. Document that instances must not share a store. 3. **`--help` / `--version` exit before taking the lock:** a help call must never supersede a running bridge. 4. **Optional, bigger: a shared daemon mode.** One long-lived bridge process holds the store/device, and each MCP client attaches to it over a local socket instead of starting its own bridge. This avoids one device per session, along with the extra logins and cross-signing that come with it. 5. **Takeover policy:** a flag (e.g. `--no-takeover`) under which a newcomer exits with a clear error instead of killing the holder. This is useful for a second, accidental start. ## Per-device cost if (1) is used without (4) Each additional device needs its own login (`credentials.json`) and cross-signing via the SSSS recovery key (`MATRIX_BRIDGE_RECOVERY_KEY`, f2c643e). Otherwise peers see an unverified device under `trust_mode: tofu`. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01XMf4EH67dMry1FyMPoDPQ1
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
public/matrix-bridge#23
No description provided.