From afbc2397d5b01f144643d7576da478a9001c5b5e Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Tue, 27 Aug 2024 14:33:44 +0100 Subject: [PATCH] Error handling in the download manager --- src/textual_serve/download_manager.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/textual_serve/download_manager.py b/src/textual_serve/download_manager.py index 2c0af65..c098da6 100644 --- a/src/textual_serve/download_manager.py +++ b/src/textual_serve/download_manager.py @@ -90,7 +90,7 @@ class DownloadManager: while True: # Request a chunk from the app service. - await app_service.send_meta( + send_result = await app_service.send_meta( { "type": "deliver_chunk_request", "key": delivery_key, @@ -98,7 +98,23 @@ class DownloadManager: } ) - chunk = await incoming_chunks.get() + if not send_result: + log.warning( + "Download {delivery_key!r} failed to request chunk from app service" + ) + del self._active_downloads[delivery_key] + break + + try: + chunk = await asyncio.wait_for(incoming_chunks.get(), DOWNLOAD_TIMEOUT) + except asyncio.TimeoutError: + log.warning( + "Download %r failed to receive chunk from app service within %r seconds", + delivery_key, + DOWNLOAD_TIMEOUT, + ) + chunk = None + if not chunk: # Empty chunk - the app process has finished sending the file. incoming_chunks.task_done()