Skip to content

Commit 91ae35e

Browse files
authored
updater.py: Better handling of timeouts during getUpdates (python-telegram-bot#1007)
TimedOut exception is an expected an normal event. To reduce noise and make things more "fluent" we now: - Make sure that we don't sleep after the timeout but rather retry immediately. - Log debug instead of error level. Fixes python-telegram-bot#802
1 parent ebcc40a commit 91ae35e

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

telegram/ext/updater.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
from telegram import Bot, TelegramError
3232
from telegram.ext import Dispatcher, JobQueue
33-
from telegram.error import Unauthorized, InvalidToken, RetryAfter
33+
from telegram.error import Unauthorized, InvalidToken, RetryAfter, TimedOut
3434
from telegram.utils.helpers import get_signal_name
3535
from telegram.utils.request import Request
3636
from telegram.utils.webhookhandler import (WebhookServer, WebhookHandler)
@@ -267,11 +267,9 @@ def start_webhook(self,
267267

268268
def _start_polling(self, poll_interval, timeout, read_latency, bootstrap_retries, clean,
269269
allowed_updates): # pragma: no cover
270-
# """
271270
# Thread target of thread 'updater'. Runs in background, pulls
272271
# updates from Telegram and inserts them in the update queue of the
273272
# Dispatcher.
274-
# """
275273

276274
cur_interval = poll_interval
277275
self.logger.debug('Updater thread started')
@@ -288,8 +286,12 @@ def _start_polling(self, poll_interval, timeout, read_latency, bootstrap_retries
288286
except RetryAfter as e:
289287
self.logger.info(str(e))
290288
cur_interval = 0.5 + e.retry_after
289+
except TimedOut as toe:
290+
self.logger.debug('Timed out getting Updates: %s', toe)
291+
# If get_updates() failed due to timeout, we should retry asap.
292+
cur_interval = 0
291293
except TelegramError as te:
292-
self.logger.error("Error while getting Updates: {0}".format(te))
294+
self.logger.error('Error while getting Updates: %s', te)
293295

294296
# Put the error into the update queue and let the Dispatcher
295297
# broadcast it
@@ -310,7 +312,8 @@ def _start_polling(self, poll_interval, timeout, read_latency, bootstrap_retries
310312

311313
cur_interval = poll_interval
312314

313-
sleep(cur_interval)
315+
if cur_interval:
316+
sleep(cur_interval)
314317

315318
@staticmethod
316319
def _increase_poll_interval(current_interval):

0 commit comments

Comments
 (0)