From 547d91dfd4aebd38dbaba898ac3c523355ccb1c4 Mon Sep 17 00:00:00 2001 From: rany Date: Fri, 17 May 2024 13:14:56 +0300 Subject: [PATCH] Fix aiohttp timeout issue This addresses the issue described in https://github.com/rany2/edge-tts/issues/190#issuecomment-2116195342 Signed-off-by: rany --- src/edge_tts/communicate.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/edge_tts/communicate.py b/src/edge_tts/communicate.py index d66a109..c53f007 100644 --- a/src/edge_tts/communicate.py +++ b/src/edge_tts/communicate.py @@ -260,7 +260,8 @@ def __init__( volume: str = "+0%", pitch: str = "+0Hz", proxy: Optional[str] = None, - receive_timeout: int = 5, + connect_timeout: int = 10, + receive_timeout: int = 60, ): """ Initializes the Communicate class. @@ -306,9 +307,14 @@ def __init__( raise TypeError("proxy must be str") self.proxy: Optional[str] = proxy - if not isinstance(receive_timeout, int): - raise TypeError("receive_timeout must be int") - self.receive_timeout: int = receive_timeout + if not isinstance(connect_timeout, int) or not isinstance(receive_timeout, int): + raise TypeError("connect_timeout and receive_timeout must be int") + self.session_timeout = aiohttp.ClientTimeout( + total=None, + connect=None, + sock_connect=connect_timeout, + sock_read=receive_timeout, + ) async def stream(self) -> AsyncGenerator[Dict[str, Any], None]: """Streams audio and metadata from the service.""" @@ -390,11 +396,11 @@ def parse_metadata() -> Dict[str, Any]: ssl_ctx = ssl.create_default_context(cafile=certifi.where()) async with aiohttp.ClientSession( trust_env=True, + timeout=self.session_timeout, ) as session, session.ws_connect( f"{WSS_URL}&ConnectionId={connect_id()}", compress=15, proxy=self.proxy, - receive_timeout=self.receive_timeout, headers={ "Pragma": "no-cache", "Cache-Control": "no-cache",