Skip to content

feat: session pool + AuthKeyDuplicatedError retry for concurrent clients on one account#152

Open
nikit34 wants to merge 1 commit into
chigwell:mainfrom
nikit34:feat/session-pool-authkey
Open

feat: session pool + AuthKeyDuplicatedError retry for concurrent clients on one account#152
nikit34 wants to merge 1 commit into
chigwell:mainfrom
nikit34:feat/session-pool-authkey

Conversation

@nikit34

@nikit34 nikit34 commented Jul 15, 2026

Copy link
Copy Markdown

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 with sys.exit(1).

Change

Opt-in session pool — set TELEGRAM_SESSION_STRINGS to 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 advisory flock, so concurrent clients deterministically pick distinct slots. The OS releases the lock automatically if a process dies.

  • Takes precedence over TELEGRAM_SESSION_STRING for the default account; a no-op unless configured, so existing single-session setups are unaffected.
  • Falls back to the first session (with a warning) when the pool is exhausted.
  • Falls back to the first session on platforms without fcntl (e.g. Windows).

Connect retry — a transient AuthKeyDuplicatedError at 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

  • Generate extra sessions with uv run session_string_generator.py.
  • Complements the existing multi-account support (TELEGRAM_SESSION_STRING_<LABEL>): that maps one client per distinct account; this maps several interchangeable sessions to one account.
  • Documented in README.md and .env.example.

Tests

New tests/test_session_pool.py covers pool parsing/dedup, flock slot claiming, exhaustion + no-fcntl fallbacks, _discover_accounts pool precedence, and the connect retry (recover + exhaust). Full suite: 145 passed.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant