From 53589ba836f472a8b0731d4f2db8d3fe2c20c7f8 Mon Sep 17 00:00:00 2001 From: Dzming Li Date: Mon, 6 Apr 2026 05:19:20 +0800 Subject: [PATCH] fix: gracefully disconnect Telegram client on exit Without calling `client.disconnect()`, the TCP connection to Telegram's servers is abandoned when the process exits. If the server restarts quickly (common with MCP stdio servers), Telegram sees two connections using the same auth key simultaneously and permanently revokes it with `AuthKeyDuplicatedError`, requiring the user to regenerate their session. Add a `finally` block to `_main()` so `client.disconnect()` is always called on exit, allowing Telegram's servers to clean up the connection state promptly. --- main.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/main.py b/main.py index 22e4f98..77a4470 100644 --- a/main.py +++ b/main.py @@ -4776,6 +4776,11 @@ async def _main() -> None: file=sys.stderr, ) sys.exit(1) + finally: + try: + await client.disconnect() + except Exception: + pass def main() -> None: