Share via

It seems that there is a gc bug in the azure speech python sdk, how to solve it?

datou ai 0 Reputation points
2026-04-05T05:14:59.8166667+00:00

I would like to use the Azure TTS input text streaming capability, as described in this documentation: https://learn.microsoft.com/zh-cn/azure/ai-services/speech-service/how-to-lower-speech-synthesis-latency?pivots=programming-language-python&source=docs#input-text-streaming

However, it seems that there are bugs in the python sdk. I use python3.13. Under the fastapi project, I found that when implementing stream tts using the azure sdk, it caused the event loop thread to be permanently stuck, which was almost an inevitable situation. This issue seems to be caused by the gc of objects in the azure sdk. Could you tell me how to solve it? When this issue occurs, the thread trace is printed as follows:

Current thread 0x000070bced8a3340 (most recent call first):
  Garbage-collecting
  File "/app/.venv/lib/python3.13/site-packages/azure/cognitiveservices/speech/interop.py", line 133 in 
  File "/usr/local/lib/python3.13/weakref.py", line 428 in 
  File "/app/.venv/lib/python3.13/site-packages/anyio/_backends/_asyncio.py", line 427 in 
  File "/app/.venv/lib/python3.13/site-packages/httpcore/_synchronization.py", line 214 in 
  File "/app/.venv/lib/python3.13/site-packages/httpcore/_async/connection_pool.py", line 343 in _close_connections
  File "/app/.venv/lib/python3.13/site-packages/httpcore/_async/connection_pool.py", line 229 in handle_async_request
  File "/app/.venv/lib/python3.13/site-packages/httpx/_transports/default.py", line 394 in handle_async_request
  File "/app/.venv/lib/python3.13/site-packages/httpx/_client.py", line 1730 in _send_single_request
  File "/app/.venv/lib/python3.13/site-packages/httpx/_client.py", line 1694 in _send_handling_redirects
  File "/app/.venv/lib/python3.13/site-packages/httpx/_client.py", line 1657 in _send_handling_auth
  File "/app/.venv/lib/python3.13/site-packages/httpx/_client.py", line 1629 in send
  File "/app/.venv/lib/python3.13/site-packages/openai/_base_client.py", line 1604 in request
  File "/app/.venv/lib/python3.13/site-packages/openinference/instrumentation/openai/_request.py", line 398 in 
  File "/app/.venv/lib/python3.13/site-packages/openai/_base_client.py", line 1884 in post
  File "/app/.venv/lib/python3.13/site-packages/openai/resources/chat/completions/completions.py", line 2714 in create
  File "/app/app/chain/general/component/agent/chain_chat_agent/agent.py", line 414 in agent_loop
  File "/usr/local/lib/python3.13/asyncio/runners.py", line 118 in run
  File "/usr/local/lib/python3.13/asyncio/runners.py", line 195 in run
  File "/app/.venv/lib/python3.13/site-packages/uvicorn/workers.py", line 107 in run
  File "/app/.venv/lib/python3.13/site-packages/gunicorn/workers/base.py", line 144 in init_process
  File "/app/.venv/lib/python3.13/site-packages/uvicorn/workers.py", line 75 in init_process
  File "/app/.venv/lib/python3.13/site-packages/gunicorn/arbiter.py", line 684 in spawn_worker
  File "/app/.venv/lib/python3.13/site-packages/gunicorn/arbiter.py", line 719 in spawn_workers
  File "/app/.venv/lib/python3.13/site-packages/gunicorn/arbiter.py", line 634 in manage_workers
  File "/app/.venv/lib/python3.13/site-packages/gunicorn/arbiter.py", line 206 in run
  File "/app/.venv/lib/python3.13/site-packages/gunicorn/app/base.py", line 71 in run
  File "/app/.venv/lib/python3.13/site-packages/gunicorn/app/base.py", line 235 in run
  File "/app/.venv/lib/python3.13/site-packages/gunicorn/app/wsgiapp.py", line 66 in run
  File "/app/.venv/bin/gunicorn", line 10 in <module>

I just follow https://learn.microsoft.com/zh-cn/azure/ai-services/speech-service/how-to-lower-speech-synthesis-latency?pivots=programming-language-python#input-text-streaming. I have considered the issue of synchronous function calls under asynchronous conditions. (Use the "run_in_threadpool" function of fastapi to submit the threadpool for asynchronous execution of synchronous function in azure sdk)

Azure AI Speech
Azure AI Speech

An Azure service that integrates speech processing into apps and services.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.