fix(mcp): file-based store takeover that works across containers #20
No reviewers
Labels
No labels
agent:felinus
agent:human
agent:maximus
merge-train
bounced
merge-train
now
merge-train
queued
merge-train
riding
priority:p0
priority:p1
priority:p2
priority:p3
status:blocked
status:needs-review
status:wip
type:chore
type:docs
type:feat
type:fix
type:perf
type:refactor
type:test
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
public/matrix-bridge!20
Loading…
Reference in a new issue
No description provided.
Delete branch "fix/store-lock-takeover"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Cause
The single-instance guard (
src/mcp/single_instance.rs) took anflockon<store>/.bridge.lock, wrote its own pid into it, and a newcomer that found the lock held rankill -TERM <pid>(then-KILL) on that pid. Claude Code starts each bridge as its owndocker compose runcontainer, so every instance lives in a separate PID namespace and shares only the bind-mounted store:p != meeven skips it when both containers happen to use the same pid (very likely, both run the same entrypoint);The
flockitself never gets released, so the newcomer polls for 8 s and exits withsingle-instance: timed out taking over the store lock.Fix
Cross-container file protocol, no pids/signals involved:
.bridge.takeoverand 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).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 theflock. Releasing the lock is the ack; the newcomer never opens the store beforetry_lock_exclusivesucceeds, so two syncing clients never share the store.proto=2marker) 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 inmcp::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