From bfa90bd880fda09e4df4ef5d7f39b81607d67578 Mon Sep 17 00:00:00 2001 From: hsn Date: Thu, 11 Jun 2026 12:31:39 +0000 Subject: [PATCH] Fix dead retry loop in asyncio_helper._process_request The RequestTimeout raise was inside the retry while-loop, so the first network error always aborted the call and MAX_RETRIES had no effect. Align the async helper with the opt-in retry convention of telebot.apihelper: add RETRY_ON_ERROR / RETRY_TIMEOUT module flags (default off, preserving current behaviour). When enabled, network errors are retried up to MAX_RETRIES times with RETRY_TIMEOUT seconds between attempts, and RequestTimeout is raised only after all attempts are exhausted. Errors reported by the Bot API itself (ApiTelegramException etc.) keep propagating immediately. Also covers the helper with self-contained unit tests. --- telebot/asyncio_helper.py | 32 +++++---- tests/test_asyncio_helper.py | 129 +++++++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+), 13 deletions(-) create mode 100644 tests/test_asyncio_helper.py diff --git a/telebot/asyncio_helper.py b/telebot/asyncio_helper.py index 2836e3ec2..485dd19db 100644 --- a/telebot/asyncio_helper.py +++ b/telebot/asyncio_helper.py @@ -1,4 +1,4 @@ -import asyncio # for future uses +import asyncio import ssl import threading import aiohttp @@ -26,6 +26,11 @@ REQUEST_TIMEOUT = 300 MAX_RETRIES = 3 +# Retrying on network errors is opt-in, mirroring telebot.apihelper: +# when RETRY_ON_ERROR is True, requests failing with a network error are +# repeated up to MAX_RETRIES times with RETRY_TIMEOUT seconds in between. +RETRY_ON_ERROR = False +RETRY_TIMEOUT = 2 REQUEST_LIMIT = 50 @@ -90,29 +95,30 @@ async def _process_request(token, url, method='get', params=None, files=None, ** params = _prepare_data(params, files) timeout = aiohttp.ClientTimeout(total=request_timeout) - got_result = False - current_try=0 + current_try = 0 + max_tries = MAX_RETRIES if RETRY_ON_ERROR else 1 session = await session_manager.get_session() - while not got_result and current_try