From 295aaa7f8a0d408c494667c1e1a08c39c556bec8 Mon Sep 17 00:00:00 2001 From: Marcel van der Veldt Date: Tue, 14 Jun 2022 19:25:16 +0200 Subject: [PATCH] Change timeout for radio connections (#365) change timeout for radio connections --- music_assistant/controllers/streams.py | 2 +- music_assistant/helpers/audio.py | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/music_assistant/controllers/streams.py b/music_assistant/controllers/streams.py index 817246f8..9e0e1544 100644 --- a/music_assistant/controllers/streams.py +++ b/music_assistant/controllers/streams.py @@ -369,7 +369,7 @@ class QueueStream: ffmpeg_proc.attach_task(writer()) - # wait max 5 seconds for all client(s) to connect + # wait max 10 seconds for all client(s) to connect try: await asyncio.wait_for(self.all_clients_connected.wait(), 10) except asyncio.exceptions.TimeoutError: diff --git a/music_assistant/helpers/audio.py b/music_assistant/helpers/audio.py index c449dda5..e70f7c8e 100644 --- a/music_assistant/helpers/audio.py +++ b/music_assistant/helpers/audio.py @@ -11,6 +11,7 @@ from time import time from typing import TYPE_CHECKING, AsyncGenerator, List, Optional, Tuple import aiofiles +from aiohttp import ClientError, ClientTimeout from music_assistant.helpers.process import AsyncProcess, check_output from music_assistant.helpers.util import create_tempfile @@ -444,11 +445,15 @@ async def get_radio_stream( ) -> AsyncGenerator[bytes, None]: """Get radio audio stream from HTTP, including metadata retrieval.""" headers = {"Icy-MetaData": "1"} + timeout = ClientTimeout(total=0, connect=10, sock_read=10) + reconnects = 0 while True: # in loop to reconnect on connection failure try: LOGGER.debug("radio stream (re)connecting to: %s", url) - async with mass.http_session.get(url, headers=headers, timeout=60) as resp: + async with mass.http_session.get( + url, headers=headers, timeout=timeout + ) as resp: headers = resp.headers meta_int = int(headers.get("icy-metaint", "0")) # stream with ICY Metadata @@ -476,8 +481,11 @@ async def get_radio_stream( else: async for chunk in resp.content.iter_any(): yield chunk - except asyncio.exceptions.TimeoutError: - pass + except (asyncio.exceptions.TimeoutError, ClientError) as err: + # reconnect on http error (max 5 times) + if reconnects >= 5: + raise err + reconnects += 1 async def get_http_stream( -- 2.34.1