Fix audio stream race condition with process reference

Capture local reference to audio_process at generator start to prevent
'NoneType' object has no attribute 'stdout' error when stop is called
concurrently from another request.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Smittix
2026-01-15 22:29:15 +00:00
parent 4af61c8cb9
commit c595450310
+8 -4
View File
@@ -883,19 +883,23 @@ def stream_audio() -> Response:
return Response(b'', mimetype='audio/mpeg', status=204)
def generate():
# Capture local reference to avoid race condition with stop
proc = audio_process
if not proc or not proc.stdout:
return
try:
while audio_running and audio_process and audio_process.poll() is None:
while audio_running and proc.poll() is None:
# Use select to avoid blocking forever
ready, _, _ = select.select([audio_process.stdout], [], [], 2.0)
ready, _, _ = select.select([proc.stdout], [], [], 2.0)
if ready:
chunk = audio_process.stdout.read(4096)
chunk = proc.stdout.read(4096)
if chunk:
yield chunk
else:
break
else:
# Timeout - check if process died
if audio_process.poll() is not None:
if proc.poll() is not None:
break
except GeneratorExit:
pass