From 08f97d413ac86b00f85e8b42aafc0661f195b8cc Mon Sep 17 00:00:00 2001 From: vrubelroman Date: Sun, 5 Jul 2026 20:00:56 +0000 Subject: [PATCH] stop infinite retry loop when user blocks the bot or deactivates account MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Forbidden from send_message was falling into the generic error handler, which resets consecutive_errors to 0 right before the send attempt — so backoff never grew past 60s and the pair retried forever, wasting request queue capacity needed by other users' real periodic checks. Co-Authored-By: Claude Sonnet 5 --- LichessClientTG_bot/bot.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/LichessClientTG_bot/bot.py b/LichessClientTG_bot/bot.py index 4425e7f..6331fea 100644 --- a/LichessClientTG_bot/bot.py +++ b/LichessClientTG_bot/bot.py @@ -13,6 +13,7 @@ from telegram.ext import ( MessageHandler, filters, ContextTypes, ConversationHandler, PicklePersistence ) +from telegram.error import Forbidden import config from config import ( @@ -1822,11 +1823,25 @@ class LichessBot: if not self.application: raise RuntimeError(f"Application not initialized, cannot send notification for {gamer['username']} to user {user_id}") - await self.application.bot.send_message( - chat_id=user_id, - text=notification, - parse_mode='Markdown' - ) + # Forbidden (user blocked the bot) is permanent, not worth retrying: + # without this, consecutive_errors gets reset to 0 above right before + # this call succeeds/fails, so the outer handler's backoff never grows + # past 60s and this pair retries forever, wasting queue capacity that + # other users' real checks need. + try: + await self.application.bot.send_message( + chat_id=user_id, + text=notification, + parse_mode='Markdown' + ) + except Forbidden: + logger.warning(f"🚫 User {user_id} blocked the bot; stopping periodic monitoring for {gamer['username']}") + self.db.set_user_gamer_period(user_id, gamer['id'], 0) + if task_key in self.periodic_tasks: + del self.periodic_tasks[task_key] + if task_key in self.period_start_times: + del self.period_start_times[task_key] + return logger.info(f"✅ Sent periodic notification for {gamer['username']} to user {user_id}") # Increment periodic notification counter self.counters.increment('periodic_notification')