On 2026-11-29 rany2/edge-tts merged PR #515 (commit 4e17d767) and shipped it in release 7.4.0 (with 7.4.1 following the same day). It fixes a bug we hit when a batch job using edge-tts stayed stuck for about 19 hours without raising an error: Communicate(..., receive_timeout=60) only bounded the HTTP upgrade handshake, not the WebSocket reads that follow it. If the remote endpoint stopped sending frames without closing the TCP socket, stream(), save() and stream_sync() never returned.

Issue

Communicate.__init__ builds an aiohttp.ClientTimeout with sock_connect=connect_timeout (default 10 s) and sock_read=receive_timeout (default 60 s), and passes it to aiohttp.ClientSession. Before #515, Communicate.__stream() then opened the WebSocket without passing a WebSocket timeout:

async with aiohttp.ClientSession(
    connector=self.connector,
    trust_env=True,
    timeout=self.session_timeout,
) as session, session.ws_connect(
    ...,
    compress=15,
    proxy=self.proxy,
    headers=DRM.headers_with_muid(WSS_HEADERS),
    ssl=_SSL_CTX,
) as websocket:
    ...
    async for received in websocket:
        ...

In aiohttp 3.10 and newer (after aio-libs/aiohttp#8444), ws_connect() explicitly clears the protocol’s read timeout once the HTTP upgrade completes whenever no WebSocket receive timeout is passed. On aiohttp 3.11+ (ClientSession._ws_connect), ws_timeout defaults to DEFAULT_WS_CLIENT_TIMEOUT where ws_receive is None:

if ws_timeout.ws_receive is None:
    conn_proto.read_timeout = None
elif conn_proto.read_timeout is not None:
    conn_proto.read_timeout = max(
        ws_timeout.ws_receive, conn_proto.read_timeout
    )

So sock_read=receive_timeout governs only the upgrade request. As soon as the connection upgrades to WebSocket, reads have no timeout at all. If the server accepts the upgrade and goes silent, or sends part of the audio and stops without sending Path:turn.end or closing the socket, async for received in websocket: blocks forever.

Fix

The change in src/edge_tts/communicate.py is 19 lines: store self.receive_timeout on Communicate and pass it to session.ws_connect() through a small helper that handles both aiohttp APIs.

def _ws_receive_timeout(receive_timeout: int) -> Dict[str, Any]:
    """Keyword arguments that apply receive_timeout to WebSocket reads.

    Since aiohttp 3.10, ws_connect() replaces the session's sock_read timeout
    with its own receive timeout (None by default) once the connection is
    upgraded, so without this a silent server makes the stream hang forever.
    """
    if hasattr(aiohttp, "ClientWSTimeout"):  # aiohttp >= 3.11
        return {
            "timeout": aiohttp.ClientWSTimeout(
                ws_receive=receive_timeout, ws_close=10.0  # aiohttp's default
            )
        }
    return {"receive_timeout": receive_timeout}

edge-tts declares aiohttp>=3.8.0,<4.0.0. aiohttp 3.11 introduced ClientWSTimeout and deprecated the receive_timeout= keyword argument on ws_connect(). Checking hasattr(aiohttp, "ClientWSTimeout") avoids deprecation warnings on aiohttp 3.11 and newer while preserving aiohttp’s default 10-second ws_close timeout, and keeps receive_timeout= working on 3.8 through 3.10. In the 7.4.0 module split (#525), the helper moved to edge_tts.network.ws_receive_timeout.

An earlier fix in the same release (bd44c1d, fixing #500) solved a related hang in stream_sync() where exceptions raised inside the worker thread were never queued to the consumer. That fix handles errors once stream() raises; #515 makes sure a stalled WebSocket raises TimeoutError in the first place.

I originally opened this patch as #486 on 2026-09-29, closed it during a quiet spell, and reopened it rebased on 7.3.1 as #515 on 2026-11-06. rany2 merged and released it in 7.4.0 on 2026-11-29.

Verification

I tested against a local aiohttp.web WebSocket server with receive_timeout=3 and an outer 7-second guard so a hang does not block forever. On aiohttp 3.14.5, comparing edge-tts 7.3.1 against 7.4.1:

[edge-tts 7.3.1, aiohttp 3.14.5, fix=absent, receive_timeout=3s]
  silent-after-upgrade  : HUNG (hit 7 s outer guard; 0 chunks)
  silent-mid-stream     : HUNG (hit 7 s outer guard; 1 chunk)
  slow-alive            : ok (1 audio chunk, 4.01 s)

[edge-tts 7.4.1, aiohttp 3.14.5, fix=present, receive_timeout=3s]
  silent-after-upgrade  : raised TimeoutError after 3.01 s (0 chunks)
  silent-mid-stream     : raised TimeoutError after 3.01 s (1 chunk)
  slow-alive            : ok (1 audio chunk, 4.01 s)

In the earlier version matrix from #486 (also with receive_timeout=3):

  • A server that stays silent after upgrade hung on aiohttp 3.9.5 through 3.14.3 and raised TimeoutError after 3 s with the fix.
  • A server that stopped mid-stream hung on aiohttp 3.10.11 through 3.14.3 and raised after 3 s with the fix.
  • A slow but alive server sending frames 2 s apart completed both before and after.
  • Normal synthesis against the live Microsoft Edge TTS endpoint was unchanged.

What remains

receive_timeout is a per-frame read timeout, not a total wall-clock cap on the whole synthesis. The slow-alive test above takes 4.01 s with receive_timeout=3 because each gap between frames is 2 s. If a server trickles frames indefinitely without ever closing, the stream stays open. Batch jobs that need a hard ceiling per utterance should still wrap save() or stream() in asyncio.timeout(...) or asyncio.wait_for(...).

Upgrade to 7.4.0 or newer (pip install --upgrade edge-tts; 7.4.1 also adds retries on early WebSocket closes). Note that 7.4.0 requires Python 3.9 or newer.

Check it yourself

verify_edge_tts_ws_timeout.py needs only edge-tts (which installs aiohttp). It runs the local WebSocket server on 127.0.0.1 with no external network calls and reproduces the three cases above in a few seconds:

python3 verify_edge_tts_ws_timeout.py

References