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.
This commit is contained in:
Dzming Li 2026-04-06 05:19:20 +08:00
parent a050889fc8
commit 53589ba836

View file

@ -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: