Prevent AuthKeyDuplicatedError from concurrent server instances - #172
Merged
Conversation
MCP clients (Claude Desktop in particular) can spawn a second telegram-mcp process before the first one exits, e.g. when restarting a connector. Two processes connecting the same Telegram session at once trips Telegram's AuthKeyDuplicatedError, which can kick out both connections. Add a per-session OS-level advisory lock (flock/msvcrt) that a process acquires before connecting. A second instance racing for the same session waits briefly for the first to release it (covers connector restarts) and otherwise exits without ever calling connect(), so the live session is never disturbed. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
marksmeed
force-pushed
the
feature/session-lock
branch
from
August 18, 2026 21:38
d5fffea to
37e62a2
Compare
chigwell
approved these changes
Aug 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
AuthKeyDuplicatedError, which can knock out both connections rather than just the newcomer.flock/msvcrt) advisory lock (telegram_mcp/singleton.py) acquired beforeconnect(). A second instance racing for the same session waits briefly (default 20s,TELEGRAM_LOCK_GRACE_SECONDS) for the first to release it — covering the connector-restart case — and otherwise exits without ever callingconnect(), so the live session is never disturbed.AuthKeyDuplicatedErrorhandling in_connect_authorized_client, which is preserved as-is for genuinely transient blips (e.g. a VPN IP change) that aren't caused by a second local instance.Test plan
uv run pytest tests/test_runner.py -v— 11 passed, including new tests for lock contention/release and independent sessions not blocking each otheruv run pytest— no new failures introduced (pre-existing failures on this Windows checkout are environment-/platform-specific and unrelated: multi-account fixtures assuming a single account, andtests/test_session_pool.pyimporting POSIX-onlyfcntl)