Command exited with code 0. Command output: --- bridge.py.bak 2026-10-29 09:03:16.527280285 +0000 +++ bridge.py 2026-10-29 09:06:31.721120565 +0000 @@ -12,9 +12,6 @@ from __future__ import annotations import asyncio -import uvloop -uvloop.install() -asyncio.set_event_loop(asyncio.new_event_loop()) import json import os import re @@ -321,7 +318,7 @@ api_hash=self.api_hash, bot_token=self.bot_token, in_memory=True, - sleep_threshold=60, + sleep_threshold=10, ) self.http: Optional[aiohttp.ClientSession] = None @@ -331,6 +328,8 @@ self.workspace_locks: dict[str, asyncio.Lock] = {} self.main_client: Optional[Client] = None self.main_client_ready = False + self._bot_restarting = False + self.watchdog_task: Optional[asyncio.Task] = None def is_admin(self, user_id: str | int) -> bool: uid = str(user_id).strip() @@ -483,6 +482,58 @@ return True return False + async def _restart_bot_client(self) -> None: + if self._bot_restarting: + return + self._bot_restarting = True + try: + log("[bot recovery] Bot client reconnect/restart triggered...") + if self.bot: + try: + await self.bot.stop() + except Exception: + pass + await asyncio.sleep(2) + self.bot = Client( + "sam_bridge_bot", + api_id=self.api_id, + api_hash=self.api_hash, + bot_token=self.bot_token, + in_memory=True, + sleep_threshold=10, + ) + self.bot.add_handler(MessageHandler(self._handle_bot_message)) + await self.bot.start() + log("[bot recovery] Bot client restarted and listening successfully!") + except Exception as exc: + log(f"[bot recovery] Failed to restart bot client: {exc}") + finally: + self._bot_restarting = False + + async def _watchdog_loop(self) -> None: + while True: + try: + await asyncio.sleep(25) + # 1. Check bot client + if self.bot is not None: + if not self.bot.is_connected and not self._bot_restarting: + log("[watchdog] Bot client is disconnected! Triggering recovery...") + asyncio.create_task(self._restart_bot_client()) + + # 2. Check main client + if self.main_client_ready and self.main_client is not None: + if not self.main_client.is_connected: + log("[watchdog] Main client is disconnected! Attempting reconnect...") + try: + await self.main_client.connect() + log("[watchdog] Main client reconnected successfully!") + except Exception as e: + log(f"[watchdog] Main client reconnect failed: {e}") + except asyncio.CancelledError: + break + except Exception as exc: + log(f"[watchdog] Loop error: {exc}") + async def send(self, chat_id: str | int, text: str, keyboard: Optional[Any] = None): if not self.bot: return None @@ -494,7 +545,10 @@ return None except Exception as exc: err_str = str(exc).lower() - if "timeout" in err_str or "connection" in err_str or "socket" in err_str: + if "the handler is closed" in err_str or "tcptransport closed" in err_str or "connection lost" in err_str: + log(f"Pyrogram transport failure detected ({exc}). Triggering bot recovery...") + asyncio.create_task(self._restart_bot_client()) + elif "timeout" in err_str or "connection" in err_str or "socket" in err_str: log(f"Pyrogram send_message to {target} raised network error ({exc}). Skipping fallback to prevent duplicate.") return None