feat: session pool + AuthKeyDuplicatedError retry for concurrent clients on one account#152
Open
nikit34 wants to merge 1 commit into
Open
feat: session pool + AuthKeyDuplicatedError retry for concurrent clients on one account#152nikit34 wants to merge 1 commit into
nikit34 wants to merge 1 commit into
Conversation
Running two MCP clients against the same Telegram account (e.g. the desktop app and a terminal CLI) can trip AuthKeyDuplicatedError: Telegram forbids one session (auth key) being used from two IPs at once, and on a VPN/dual-stack host two local clients egress via different source IPs. Add an opt-in pool of interchangeable sessions via TELEGRAM_SESSION_STRINGS (whitespace/comma/semicolon separated). Each process claims the first session not already held by a live client via an advisory flock, so clients deterministically pick distinct slots; the OS frees the lock if a process dies. The pool takes precedence over TELEGRAM_SESSION_STRING for the default account and is a no-op unless configured. Falls back to the first session (with a warning) when the pool is exhausted, and to the first session on platforms without fcntl (e.g. Windows). As a safety net, a transient AuthKeyDuplicatedError at connect time is retried with exponential backoff before the server gives up. Documented in README and .env.example; covered by tests/test_session_pool.py. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Problem
Running two MCP clients against the same Telegram account at once (e.g. the Claude Desktop app and a terminal CLI) can trip
AuthKeyDuplicatedError. Telegram forbids one session (auth key) being used from two IPs simultaneously, and on a VPN / dual-stack host two local clients can egress via different source IPs and collide. When it happens on connect, the server currently dies withsys.exit(1).Change
Opt-in session pool — set
TELEGRAM_SESSION_STRINGSto several interchangeable session strings for one account (whitespace / comma / semicolon separated). Each process claims the first session not already held by a live client via an advisoryflock, so concurrent clients deterministically pick distinct slots. The OS releases the lock automatically if a process dies.TELEGRAM_SESSION_STRINGfor thedefaultaccount; a no-op unless configured, so existing single-session setups are unaffected.fcntl(e.g. Windows).Connect retry — a transient
AuthKeyDuplicatedErrorat connect time (e.g. during a VPN reconnect) is now retried with exponential backoff before the server gives up, instead of taking the whole MCP server down.Notes
uv run session_string_generator.py.TELEGRAM_SESSION_STRING_<LABEL>): that maps one client per distinct account; this maps several interchangeable sessions to one account.README.mdand.env.example.Tests
New
tests/test_session_pool.pycovers pool parsing/dedup, flock slot claiming, exhaustion + no-fcntlfallbacks,_discover_accountspool precedence, and the connect retry (recover + exhaust). Full suite:145 passed.