Skip to content

Server crashes during shutdown when Telegram connection fails #6

@vadlml

Description

@vadlml

Description

The MCP Telegram server crashes during shutdown with connection-related
errors when the initial Telegram connection fails or is interrupted. This
occurs because the app_lifespan context manager always attempts to
disconnect the client, even when the connection was never successfully
established.

Steps to Reproduce

  1. Start the MCP server with invalid API credentials or during network
    issues
  2. Initial connection to Telegram fails
  3. Attempt to shut down the server (Ctrl+C or normal shutdown)
  4. Server crashes with connection-related errors during cleanup

Expected Behavior

The server should shut down gracefully regardless of connection state,
only attempting to disconnect when a connection was successfully
established.

Actual Behavior

Server crashes during shutdown because tg.client.disconnect() is called
unconditionally in the finally block, even when:

  • The initial tg.client.connect() call failed
  • The client was never successfully connected
  • The client is already disconnected or in an invalid state

Environment

  • MCP Telegram version: 0.1.11
  • Python version: 3.10+
  • Platform: Any

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions