fix(mcp): file-based store takeover that works across containers #20

Merged
jmz merged 1 commit from fix/store-lock-takeover into main 2026-10-06 22:25:48 +00:00
Owner

Cause

The single-instance guard (src/mcp/single_instance.rs) took an flock on <store>/.bridge.lock, wrote its own pid into it, and a newcomer that found the lock held ran kill -TERM <pid> (then -KILL) on that pid. Claude Code starts each bridge as its own docker compose run container, so every instance lives in a separate PID namespace and shares only the bind-mounted store:

  • the pid in the file is a container-local pid; in the newcomer's namespace it names a different process or none, and p != me even skips it when both containers happen to use the same pid (very likely, both run the same entrypoint);
  • the holder had no cooperative path at all: nothing in it ever looked at the lock file again.

The flock itself never gets released, so the newcomer polls for 8 s and exits with single-instance: timed out taking over the store lock.

Fix

Cross-container file protocol, no pids/signals involved:

  1. Newcomer finds the lock held, writes .bridge.takeover and refreshes it every 250 ms (its mtime is a heartbeat, so a request left by a crashed newcomer expires after 5 s and never evicts a healthy holder).
  2. The holder polls for a fresh request (serve_handover). It then takes the client write lock (waits for in-flight tool calls, new calls queue behind it), aborts and joins the sync task, drops the client (closing the SDK/sqlite stores), and only then releases the flock. Releasing the lock is the ack; the newcomer never opens the store before try_lock_exclusive succeeds, so two syncing clients never share the store.
  3. The old process stays up as a no-op MCP server: every tool returns "This session's Matrix connection was taken over by a newer session; reconnect with /mcp." (it exits on stdin EOF as before).
  4. If an in-flight tool call does not finish within 30 s the holder exits the process instead (sqlite commits are atomic), which still guarantees one writer.
  5. A holder from the previous version (lock file is a bare pid, no proto=2 marker) cannot answer; the newcomer fails after ~2 s with an explicit error naming the problem instead of a generic timeout. Newcomer timeout is now 60 s.

The CLI commands that call acquire_store_lock (verify-own-devices, enable-recovery) use the same protocol and keep superseding a running bridge.

Tests

cargo test (8 new tests in mcp::single_instance): uncontended acquire, request freshness/staleness, request visible to holder, timeout when holder never polls (and request cleaned up), legacy holder fail-fast, stale request not evicting a new holder, two-instance handover ordering (old instance finishes stopping before new one acquires), chain of three takeovers leaving exactly one holder. They use temp dirs only.

Deployment note

Already-running bridges from older images (e.g. 0.2.1) do not poll, so they cannot be taken over by this change; they must be stopped once. After rolling this out, every later session takes over cleanly.

🤖 Generated with Claude Code

https://claude.ai/code/session_01B8woGafdDuKYnXajcXG9Ar

## Cause The single-instance guard (`src/mcp/single_instance.rs`) took an `flock` on `<store>/.bridge.lock`, wrote its own pid into it, and a newcomer that found the lock held ran `kill -TERM <pid>` (then `-KILL`) on that pid. Claude Code starts each bridge as its own `docker compose run` container, so every instance lives in a separate PID namespace and shares only the bind-mounted store: - the pid in the file is a container-local pid; in the newcomer's namespace it names a different process or none, and `p != me` even skips it when both containers happen to use the same pid (very likely, both run the same entrypoint); - the holder had no cooperative path at all: nothing in it ever looked at the lock file again. The `flock` itself never gets released, so the newcomer polls for 8 s and exits with `single-instance: timed out taking over the store lock`. ## Fix Cross-container file protocol, no pids/signals involved: 1. Newcomer finds the lock held, writes `.bridge.takeover` and refreshes it every 250 ms (its mtime is a heartbeat, so a request left by a crashed newcomer expires after 5 s and never evicts a healthy holder). 2. The holder polls for a fresh request (`serve_handover`). It then takes the client write lock (waits for in-flight tool calls, new calls queue behind it), aborts and joins the sync task, drops the client (closing the SDK/sqlite stores), and only then releases the `flock`. Releasing the lock is the ack; the newcomer never opens the store before `try_lock_exclusive` succeeds, so two syncing clients never share the store. 3. The old process stays up as a no-op MCP server: every tool returns "This session's Matrix connection was taken over by a newer session; reconnect with /mcp." (it exits on stdin EOF as before). 4. If an in-flight tool call does not finish within 30 s the holder exits the process instead (sqlite commits are atomic), which still guarantees one writer. 5. A holder from the previous version (lock file is a bare pid, no `proto=2` marker) cannot answer; the newcomer fails after ~2 s with an explicit error naming the problem instead of a generic timeout. Newcomer timeout is now 60 s. The CLI commands that call `acquire_store_lock` (`verify-own-devices`, `enable-recovery`) use the same protocol and keep superseding a running bridge. ## Tests `cargo test` (8 new tests in `mcp::single_instance`): uncontended acquire, request freshness/staleness, request visible to holder, timeout when holder never polls (and request cleaned up), legacy holder fail-fast, stale request not evicting a new holder, two-instance handover ordering (old instance finishes stopping before new one acquires), chain of three takeovers leaving exactly one holder. They use temp dirs only. ## Deployment note Already-running bridges from older images (e.g. 0.2.1) do not poll, so they cannot be taken over by this change; they must be stopped once. After rolling this out, every later session takes over cleanly. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01B8woGafdDuKYnXajcXG9Ar
fix(mcp): file-based store takeover that works across containers
All checks were successful
check / check (pull_request) Successful in 4m7s
check / check (push) Successful in 4m9s
d3d6db8543
The single-instance guard signalled the lock holder with kill(pid), but each
'docker compose run' has its own PID namespace, so the pid stored in the lock
file means nothing to a newer container. Replace it with a request/release
protocol over files in the shared store dir: the newcomer writes a heartbeat
.bridge.takeover request, the holder polls it, stops sync, closes the client
and only then releases the flock. The old instance stays up as a no-op MCP
server answering tools with a takeover error.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01B8woGafdDuKYnXajcXG9Ar
jmz merged commit d3d6db8543 into main 2026-10-06 22:25:48 +00:00
jmz deleted branch fix/store-lock-takeover 2026-10-06 22:25:49 +00:00
Sign in to join this conversation.
No reviewers
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!20
No description provided.