Command exited with code 0. Command output: --- bridge.py.bak2 2026-11-01 17:22:01.615900372 +0000 +++ bridge.py 2026-11-01 17:23:16.940257515 +0000 @@ -20,6 +20,7 @@ from dataclasses import asdict, dataclass, field from datetime import datetime, timedelta, timezone from pathlib import Path +from types import SimpleNamespace from typing import Any, Optional import aiohttp @@ -488,44 +489,61 @@ self._bot_restarting = True try: log("[bot recovery] Bot client reconnect/restart triggered...") - if self.bot: + old_bot = self.bot + if old_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!") + await asyncio.wait_for(old_bot.stop(), timeout=4.0) + except Exception as exc: + log(f"[bot recovery] old_bot.stop() note: {exc}") + + await asyncio.sleep(1) + + success = False + for attempt in range(1, 4): + try: + new_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, + ) + new_bot.add_handler(MessageHandler(self._handle_bot_message)) + await asyncio.wait_for(new_bot.start(), timeout=12.0) + self.bot = new_bot + success = True + log(f"[bot recovery] Bot client restarted and listening successfully! (attempt {attempt})") + break + except Exception as exc: + log(f"[bot recovery] Attempt {attempt} failed: {exc}") + if attempt < 3: + await asyncio.sleep(2) + + if not success: + log("[bot recovery] Recovery attempts failed/timed out. Watchdog will retry on next cycle.") except Exception as exc: - log(f"[bot recovery] Failed to restart bot client: {exc}") + log(f"[bot recovery] Unexpected error in restart: {exc}") finally: self._bot_restarting = False async def _watchdog_loop(self) -> None: while True: try: - await asyncio.sleep(25) + await asyncio.sleep(20) # 1. Check bot client if self.bot is not None: - if not self.bot.is_connected and not self._bot_restarting: + is_connected = getattr(self.bot, "is_connected", False) + if not 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: + if not getattr(self.main_client, "is_connected", False): log("[watchdog] Main client is disconnected! Attempting reconnect...") try: - await self.main_client.connect() + await asyncio.wait_for(self.main_client.connect(), timeout=10.0) log("[watchdog] Main client reconnected successfully!") except Exception as e: log(f"[watchdog] Main client reconnect failed: {e}") @@ -534,42 +552,85 @@ 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: + async def _send_http_fallback(self, target: str | int, text: str, keyboard: Optional[Any] = None) -> Optional[Any]: