Command exited with code 1. 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 log(f"Pyrogram send_message to {target} failed: {exc}. Trying HTTP Bot API fallback...") @@ -540,7 +594,7 @@ api_id=self.api_id, api_hash=self.api_hash, phone_number=self.owner_phone or None, - sleep_threshold=180, + sleep_threshold=10, ) def _quarantine_main_session_files(self) -> None: @@ -622,6 +676,7 @@ self.main_client_ready = False log("Bot service started.") + self.watchdog_task = asyncio.create_task(self._watchdog_loop()) for path in USERS_DIR.glob("*.json"): if path.is_file(): @@ -631,12 +686,16 @@ self._seed_existing_firebases() - for user_id, ws in list(self.workspaces.items()): - if ws.active and ws.device_id: - try: - await self.ensure_bridge_runtime(ws) - except Exception as exc: - log(f"Failed to restore bridge runtime for user {user_id}: {exc}") + active_workspaces = [ws for ws in self.workspaces.values() if ws.active and ws.device_id] + log(f"Restoring bridge runtimes for {len(active_workspaces)} active user(s)...") + pin_delay = 0.5 + for ws in active_workspaces: + try: + await self.ensure_bridge_runtime(ws, defer_pin=True) + asyncio.create_task(self._delayed_auto_pin(ws, delay=pin_delay)) + pin_delay += 0.5 + except Exception as exc: + log(f"Failed to restore bridge runtime for user {ws.user_id}: {exc}") log("Ready for multi-user menu flow.") await asyncio.Event().wait() @@ -706,6 +765,9 @@ log(f"[fcm sync] Synced old FCM credentials for {synced} user(s)") async def shutdown(self) -> None: + if self.watchdog_task: + self.watchdog_task.cancel() + for task in list(self.bridge_tasks.values()): task.cancel() @@ -753,7 +815,15 @@ ) await self.send(other_uid, fmt_msg("Session Paused", body, error=True)) - async def ensure_bridge_runtime(self, ws: Workspace) -> None: + async def _delayed_auto_pin(self, ws: Workspace, delay: float = 0.0) -> None: + if delay > 0: + await asyncio.sleep(delay) + try: + await self._auto_pin_status(ws) + except Exception as exc: + log(f"[pin] deferred pin failed for {ws.user_id}: {exc}") + + async def ensure_bridge_runtime(self, ws: Workspace, defer_pin: bool = False) -> None: if not ws.active or not ws.device_id: return @@ -774,7 +844,8 @@ self.bridge_tasks[user_id] = task log(f"Bridge started for user {user_id} ({ws.active_mode}).") - await self._auto_pin_status(ws) + if not defer_pin: + await self._auto_pin_status(ws) async def refresh_bridge_runtime(self, ws: Workspace) -> None: user_id = ws.user_id @@ -2446,8 +2517,9 @@ url += f"?auth={ws.firebase_auth}" cutoff_ts = ws.last_processed_ts + fb_timeout = aiohttp.ClientTimeout(total=5, sock_connect=2) if not cutoff_ts: - async with self.http.get(url, timeout=8) as seed: + async with self.http.get(url, timeout=fb_timeout) as seed: if seed.status == 200: try: seed_data = await seed.json() @@ -2471,7 +2543,7 @@ await asyncio.sleep(poll_interval) continue - async with self.http.get(url, timeout=10) as r: + async with self.http.get(url, timeout=fb_timeout) as r: if r.status != 200: consecutive_errors += 1 if r.status in (401, 403, 404, 423): @@ -2545,7 +2617,8 @@ log(f"[fb poll {user_id}] Host connection/lookup failed: {exc}. Backing off 30s...") await asyncio.sleep(30) else: - log(f"[fb poll {user_id}] {exc}") + err_msg = str(exc) or type(exc).__name__ + log(f"[fb poll {user_id}] {err_msg}") await asyncio.sleep(max(2.0, poll_interval * 2)) def _latest_remote_ts(self, data: dict[str, Any]) -> int: