Repository: Significant-Gravitas/Auto-GPT
File: autogpt_platform/backend/backend/util/cloud_storage.py (Python)

Code before:
            f"in_task: {current_task is not None}"
        )

        # Create a reusable session with proper configuration
        # Key fix: Don't set timeout on session, let gcloud-aio handle it
        self._session = aiohttp.ClientSession(
            connector=aiohttp.TCPConnector(
                limit=100,  # Connection pool limit
                force_close=False,  # Reuse connections
                enable_cleanup_closed=True,
            )
        )

        # Create the GCS client with our session
        # The key is NOT setting timeout on the session but letting the library handle it
        self._async_gcs_client = async_gcs_storage.Storage(session=self._session)

        return self._async_gcs_client

    async def close(self):
        """Close all client connections properly."""
        if self._async_gcs_client is not None:
            try:
                await self._async_gcs_client.close()
            except Exception as e:
                logger.debug(f"Error closing GCS client: {e}")
            self._async_gcs_client = None

        if self._session is not None:
            try:
                await self._session.close()
            except Exception as e:
                logger.debug(f"Error closing session: {e}")
            self._session = None

    async def __aenter__(self):
        """Async context manager entry."""
        return self

    async def __aexit__(self, exc_type, exc_val, exc_tb):
        """Async context manager exit."""
        _ = (
            exc_type,
            exc_val,
            exc_tb,
        )  # Unused but required for context manager protocol
        await self.close()

    def _get_sync_gcs_client(self):
        """Lazy initialization of sync GCS client (only for signed URLs)."""
        if self._sync_gcs_client is None:

Review comment (bug):
debug is practically useless right now, it's too verbose to even analyze, let's just use warn or error here

Code after:
        self._session = aiohttp.ClientSession(
            connector=aiohttp.TCPConnector(
                limit=100,  # Connection pool limit
                force_close=False,  # Reuse connections
                enable_cleanup_closed=True,
            )
        )

        # Create the GCS client with our session
        # The key is NOT setting timeout on the session but letting the library handle it
        self._async_gcs_client = async_gcs_storage.Storage(session=self._session)

        return self._async_gcs_client

    async def close(self):
        """Close all client connections properly."""
        if self._async_gcs_client is not None:
            try:
                await self._async_gcs_client.close()
            except Exception as e:
                logger.warning(f"[CloudStorage] Error closing GCS client: {e}")
            self._async_gcs_client = None

        if self._session is not None:
            try:
                await self._session.close()
            except Exception as e:
                logger.warning(f"[CloudStorage] Error closing session: {e}")
            self._session = None

    async def __aenter__(self):
        """Async context manager entry."""
        return self

    async def __aexit__(self, exc_type, exc_val, exc_tb):
        """Async context manager exit."""
        await self.close()

    def _get_sync_gcs_client(self):
        """Lazy initialization of sync GCS client (only for signed URLs)."""
        if self._sync_gcs_client is None:
            # Use Application Default Credentials (ADC) - same as media.py
            self._sync_gcs_client = gcs_storage.Client()
        return self._sync_gcs_client

    def parse_cloud_path(self, path: str) -> Tuple[str, str]:
        """
        Parse a cloud storage path and return provider and actual path.

        Args:
            path: Cloud storage path (e.g., "gcs://bucket/path/to/file")