Skip to content

fix(telegram): route PTB NetworkError to reconnect ladder in polling heartbeat - #62060

Closed
liuhao1024 wants to merge 1 commit into
NousResearch:mainfrom
liuhao1024:liuhao/cron-bugfix-62047
Closed

fix(telegram): route PTB NetworkError to reconnect ladder in polling heartbeat#62060
liuhao1024 wants to merge 1 commit into
NousResearch:mainfrom
liuhao1024:liuhao/cron-bugfix-62047

Conversation

@liuhao1024

Copy link
Copy Markdown
Contributor

What does this PR do?

The persistent Telegram polling heartbeat (_polling_heartbeat_loop) did not recognize python-telegram-bot (PTB) NetworkError or TimedOut exceptions as connectivity failures. These PTB exceptions inherit from TelegramError, not OSError, so the existing handler (asyncio.TimeoutError, OSError) missed them. A broad except Exception block subsequently swallowed these errors silently, preventing the gateway from detecting and recovering dead TCP connections.

This fix extracts the reconnect trigger logic into a helper and explicitly checks for PTB NetworkError / TimedOut in the exception handler, routing them to the existing reconnect ladder. Non-connectivity PTB errors (e.g., BadRequest, InvalidToken) remain swallowed as before.

Related Issue

Fixes #62047

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)

Changes Made

  • plugins/platforms/telegram/adapter.py: Modified _polling_heartbeat_loop to catch and handle telegram.error.NetworkError and telegram.error.TimedOut as connectivity failures. Added a local helper _trigger_reconnect to deduplicate the reconnect scheduling logic.
  • tests/gateway/test_telegram_network_reconnect.py: Added regression tests for PTB NetworkError, TimedOut (trigger reconnect) and BadRequest (ignore).

How to Test

  1. Run the new regression tests: pytest tests/gateway/test_telegram_network_reconnect.py::test_heartbeat_loop_triggers_reconnect_on_ptb_network_error tests/gateway/test_telegram_network_reconnect.py::test_heartbeat_loop_triggers_reconnect_on_ptb_timed_out tests/gateway/test_telegram_network_reconnect.py::test_heartbeat_loop_ignores_ptb_bad_request
  2. Run the full Telegram network reconnect test suite to ensure existing behavior is preserved: pytest tests/gateway/test_telegram_network_reconnect.py
  3. Observed result: All 3 new tests pass, confirming that PTB NetworkError and TimedOut now trigger _handle_polling_network_error (task created), while BadRequest does not. Existing tests for OSError and asyncio.TimeoutError continue to pass.

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix(scope):, feat(scope):, etc.)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix/feature (no unrelated commits)
  • I've run pytest tests/ -q and all tests pass (ran subset for gateway/telegram).
  • I've added tests for my changes (required for bug fixes, strongly encouraged for features)
  • I've tested on my platform: macOS (via local pytest run).

Documentation & Housekeeping

  • I've updated relevant documentation (README, docs/, docstrings) — or N/A (added comments in code)
  • I've updated cli-config.yaml.example if I added/changed config keys — or N/A
  • I've updated CONTRIBUTING.md or AGENTS.md if I changed architecture or workflows — or N/A
  • I've considered cross-platform impact (Windows, macOS) per the compatibility guide — or N/A (platform-agnostic exception handling)
  • I've updated tool descriptions/schemas if I changed tool behavior — or N/A

…heartbeat

The persistent Telegram polling heartbeat (_polling_heartbeat_loop) did not
recognize python-telegram-bot (PTB) NetworkError or TimedOut exceptions as
connectivity failures because these classes inherit from TelegramError, not
OSError. The existing handler caught (asyncio.TimeoutError, OSError) and then
silently swallowed all other exceptions in a broad except Exception block.

This caused genuine PTB connectivity failures to be ignored, preventing the
gateway from recovering silently dead connections.

Fix:
1. Extracted the reconnect trigger logic into a helper (_trigger_reconnect).
2. Added explicit isinstance checks for telegram.error.NetworkError and
   TimedOut in the except Exception block.
3. Routed PTB connectivity errors through the reconnect ladder, while
   preserving the swallow behavior for non-connectivity errors (e.g.
   BadRequest, InvalidToken).

Tests:
Added regression tests for:
- PTB NetworkError -> triggers reconnect
- PTB TimedOut -> triggers reconnect
- PTB BadRequest -> ignored (non-connectivity)

Fixes NousResearch#62047
@alt-glitch alt-glitch added type/bug Something isn't working P1 High — major feature broken, no workaround comp/gateway Gateway runner, session dispatch, delivery platform/telegram Telegram bot adapter sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages labels Jul 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery P1 High — major feature broken, no workaround platform/telegram Telegram bot adapter sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Telegram polling heartbeat swallows python-telegram-bot NetworkError, so reconnect is never triggered

2 participants