From dd55e7c74801421d26e1f590811a9168ff4c607c Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Thu, 11 Aug 2022 10:53:21 +0200 Subject: [PATCH] Prevent double-logging of 'timeout for updates' --- telethon/_updates/messagebox.py | 5 ++++- telethon/client/updates.py | 16 +++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/telethon/_updates/messagebox.py b/telethon/_updates/messagebox.py index 9aeeef2b..bfd84540 100644 --- a/telethon/_updates/messagebox.py +++ b/telethon/_updates/messagebox.py @@ -250,7 +250,10 @@ class MessageBox: elif self.next_deadline in self.map: deadline = min(deadline, self.map[self.next_deadline].deadline) - if now > deadline: + # asyncio's loop time precision only seems to be about 3 decimal places, so it's possible that + # we find the same number again on repeated calls. Without the "or equal" part we would log the + # timeout for updates several times (it also makes sense to get difference if now is the deadline). + if now >= deadline: # Check all expired entries and add them to the list that needs getting difference. self.getting_diff_for.update(entry for entry, gap in self.possible_gaps.items() if now > gap.deadline) self.getting_diff_for.update(entry for entry, state in self.map.items() if now > state.deadline) diff --git a/telethon/client/updates.py b/telethon/client/updates.py index 7cfb1e63..82b5b1bf 100644 --- a/telethon/client/updates.py +++ b/telethon/client/updates.py @@ -335,13 +335,15 @@ class UpdateMethods: continue deadline = self._message_box.check_deadlines() - try: - updates = await asyncio.wait_for( - self._updates_queue.get(), - deadline - asyncio.get_running_loop().time() - ) - except asyncio.TimeoutError: - self._log[__name__].info('Timeout waiting for updates expired') + deadline_delay = deadline - asyncio.get_running_loop().time() + if deadline_delay > 0: + # Don't bother sleeping and timing out if the delay is already 0 (pollutes the logs). + try: + updates = await asyncio.wait_for(self._updates_queue.get(), deadline_delay) + except asyncio.TimeoutError: + self._log[__name__].info('Timeout waiting for updates expired') + continue + else: continue processed = []