Repository: exo-explore/exo
File: src/exo/download/coordinator.py (Python)

Code before:
            tg.start_soon(self._check_internet_connection)

    def _test_internet_connection(self) -> None:
        try:
            socket.create_connection(("1.1.1.1", 443), timeout=3).close()
            self.shard_downloader.set_internet_connection(True)
        except OSError:
            self.shard_downloader.set_internet_connection(False)
        logger.debug(
            f"Internet connectivity: {self.shard_downloader.internet_connection}"
        )

    async def _check_internet_connection(self) -> None:
        first_connection = True
        while True:
            await asyncio.sleep(10)
            self._test_internet_connection()
            if first_connection and self.shard_downloader.internet_connection:
                first_connection = False
                self._tg.start_soon(self._emit_existing_download_progress)

    def shutdown(self) -> None:
        self._tg.cancel_scope.cancel()

    async def _command_processor(self) -> None:
        with self.download_command_receiver as commands:
            async for cmd in commands:
                # Only process commands targeting this node
                if cmd.command.target_node_id != self.node_id:
                    continue

                match cmd.command:
                    case StartDownload(shard_metadata=shard):
                        await self._start_download(shard)
                    case DeleteDownload(model_id=model_id):
                        await self._delete_download(model_id)

    async def _start_download(self, shard: ShardMetadata) -> None:
        model_id = shard.model_card.model_id

        # Check if already downloading or complete
        if model_id in self.download_status:
            status = self.download_status[model_id]
            if isinstance(status, (DownloadOngoing, DownloadCompleted)):
                logger.debug(
                    f"Download for {model_id} already in progress or complete, skipping"
                )
                return

        # Emit pending status
        progress = DownloadPending(shard_metadata=shard, node_id=self.node_id)

Review comment (suggestion):
I think we should return from this once we get a success, and then use errors elsewhere in downloads to imply we don't have Internet.

Code after:
            tg.start_soon(self._check_internet_connection)

    def _test_internet_connection(self) -> None:
        try:
            socket.create_connection(("1.1.1.1", 443), timeout=3).close()
            self.shard_downloader.set_internet_connection(True)
        except OSError:
            self.shard_downloader.set_internet_connection(False)
        logger.debug(
            f"Internet connectivity: {self.shard_downloader.internet_connection}"
        )

    async def _check_internet_connection(self) -> None:
        first_connection = True
        while True:
            await asyncio.sleep(10)

            # Assume that internet connection is set to False on 443 errors.
            if self.shard_downloader.internet_connection:
                continue

            self._test_internet_connection()

            if first_connection and self.shard_downloader.internet_connection:
                first_connection = False
                self._tg.start_soon(self._emit_existing_download_progress)

    def shutdown(self) -> None:
        self._tg.cancel_scope.cancel()

    async def _command_processor(self) -> None:
        with self.download_command_receiver as commands:
            async for cmd in commands:
                # Only process commands targeting this node
                if cmd.command.target_node_id != self.node_id:
                    continue

                match cmd.command:
                    case StartDownload(shard_metadata=shard):
                        await self._start_download(shard)
                    case DeleteDownload(model_id=model_id):
                        await self._delete_download(model_id)

    async def _start_download(self, shard: ShardMetadata) -> None:
        model_id = shard.model_card.model_id

        # Check if already downloading or complete
        if model_id in self.download_status:
            status = self.download_status[model_id]
            if isinstance(status, (DownloadOngoing, DownloadCompleted)):
                logger.debug(