fix(telegram): route PTB NetworkError to reconnect ladder in polling heartbeat - #62060
Closed
liuhao1024 wants to merge 1 commit into
Closed
fix(telegram): route PTB NetworkError to reconnect ladder in polling heartbeat#62060liuhao1024 wants to merge 1 commit into
liuhao1024 wants to merge 1 commit into
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
The persistent Telegram polling heartbeat (
_polling_heartbeat_loop) did not recognize python-telegram-bot (PTB)NetworkErrororTimedOutexceptions as connectivity failures. These PTB exceptions inherit fromTelegramError, notOSError, so the existing handler(asyncio.TimeoutError, OSError)missed them. A broadexcept Exceptionblock 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/TimedOutin 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
Changes Made
plugins/platforms/telegram/adapter.py: Modified_polling_heartbeat_loopto catch and handletelegram.error.NetworkErrorandtelegram.error.TimedOutas connectivity failures. Added a local helper_trigger_reconnectto deduplicate the reconnect scheduling logic.tests/gateway/test_telegram_network_reconnect.py: Added regression tests for PTBNetworkError,TimedOut(trigger reconnect) andBadRequest(ignore).How to Test
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_requestpytest tests/gateway/test_telegram_network_reconnect.pyNetworkErrorandTimedOutnow trigger_handle_polling_network_error(task created), whileBadRequestdoes not. Existing tests forOSErrorandasyncio.TimeoutErrorcontinue to pass.Checklist
Code
fix(scope):,feat(scope):, etc.)pytest tests/ -qand all tests pass (ran subset for gateway/telegram).Documentation & Housekeeping
docs/, docstrings) — or N/A (added comments in code)cli-config.yaml.exampleif I added/changed config keys — or N/ACONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — or N/A