{"record":{"id":"10e05643aae2e48e","repo":"harry0703/MoneyPrinterTurbo","slug":"edge-tts-stream-timed-out-after-timeout-seconds-g","errorCode":null,"errorMessage":"edge_tts stream timed out after {timeout_seconds:g}s","messagePattern":"edge_tts stream timed out after (.+?)s","errorType":"exception","errorClass":"TimeoutError","httpStatus":null,"severity":"error","filePath":"app/services/voice.py","lineNumber":703,"sourceCode":"    stream_queue = queue.Queue()\n    done_marker = object()\n\n    def _produce_chunks():\n        try:\n            for chunk in communicate.stream_sync():\n                stream_queue.put((\"chunk\", chunk))\n            stream_queue.put((\"done\", done_marker))\n        except Exception as e:\n            stream_queue.put((\"error\", e))\n\n    thread = threading.Thread(target=_produce_chunks, daemon=True)\n    thread.start()\n\n    deadline = time.monotonic() + timeout_seconds\n    while True:\n        remaining_seconds = deadline - time.monotonic()\n        if remaining_seconds <= 0:\n            raise TimeoutError(\n                f\"edge_tts stream timed out after {timeout_seconds:g}s\"\n            )\n\n        try:\n            item_type, payload = stream_queue.get(\n                timeout=min(0.5, remaining_seconds)\n            )\n        except queue.Empty:\n            continue\n\n        if item_type == \"chunk\":\n            on_chunk(payload)\n        elif item_type == \"error\":\n            raise payload\n        elif item_type == \"done\":\n            return\n\n","sourceCodeStart":685,"sourceCodeEnd":721,"githubUrl":"https://github.com/harry0703/MoneyPrinterTurbo/blob/1f9f19c2021a68d04df228f33e9099a0c947f6f8/app/services/voice.py#L685-L721","documentation":"Raised by a watchdog loop around an edge-tts chunk stream. Chunks are produced on a daemon thread into a queue; the main thread polls the queue with short timeouts against a monotonic deadline. If the deadline expires before a 'done' or 'error' item arrives (i.e. no item of any kind is delivered in time), a TimeoutError is raised.","triggerScenarios":"Calling the edge_tts streaming wrapper with a small timeout_seconds while the network to Microsoft's TTS endpoint is slow or stalled; edge-tts connection established but no audio chunks arrive (service throttling/queueing); producer thread blocked inside Communicate.stream() awaiting the first bytes.","commonSituations":"Edge TTS transient outages or regional throttling; proxies/firewalls that allow the WebSocket handshake but stall the stream; timeout_seconds configured too aggressively for long texts where the first chunk takes a while; edge-tts library version change altering stream behavior after token/endpoint updates.","solutions":["Retry the request — edge-tts stream stalls are frequently transient; the caller should implement retry with backoff.","Increase timeout_seconds so slow first-chunk latency does not trip the deadline.","Check network egress to speech.platform.bing.com (proxy, firewall, DNS) from the host.","Upgrade the edge-tts package if Microsoft changed endpoints or auth tokens (a common breakage source)."],"exampleFix":"# before\nsubmaker = edge_tts_stream(text, voice, voice_file, timeout_seconds=5)\n\n# after: tolerate slow first chunk and retry transient stalls\nfor attempt in range(3):\n    try:\n        submaker = edge_tts_stream(text, voice, voice_file, timeout_seconds=30)\n        break\n    except TimeoutError:\n        if attempt == 2:\n            raise","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"for attempt in range(3):\n    try:\n        return edge_tts_stream(text, voice_name, voice_file,\n                               timeout_seconds=30)\n    except TimeoutError:\n        if attempt == 2:\n            raise\n        time.sleep(2 ** attempt)","preventionTips":["Set timeout_seconds generously relative to text length and first-chunk latency.","Monitor edge-tts package updates — endpoint/auth changes frequently cause stream stalls.","Ensure unobstructed egress to the Edge TTS WebSocket endpoint (proxy/DNS)."],"tags":["tts","edge-tts","network","timeout","streaming"],"backgroundTag":null,"analyzedSha":"1f9f19c2021a68d04df228f33e9099a0c947f6f8","analyzedAt":"2026-08-14T19:41:05.568Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}