mirror of
https://github.com/smittix/intercept.git
synced 2026-07-10 10:38:12 -07:00
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:
@@ -883,19 +883,23 @@ def stream_audio() -> Response:
|
|||||||
return Response(b'', mimetype='audio/mpeg', status=204)
|
return Response(b'', mimetype='audio/mpeg', status=204)
|
||||||
|
|
||||||
def generate():
|
def generate():
|
||||||
|
# Capture local reference to avoid race condition with stop
|
||||||
|
proc = audio_process
|
||||||
|
if not proc or not proc.stdout:
|
||||||
|
return
|
||||||
try:
|
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
|
# Use select to avoid blocking forever
|
||||||
ready, _, _ = select.select([audio_process.stdout], [], [], 2.0)
|
ready, _, _ = select.select([proc.stdout], [], [], 2.0)
|
||||||
if ready:
|
if ready:
|
||||||
chunk = audio_process.stdout.read(4096)
|
chunk = proc.stdout.read(4096)
|
||||||
if chunk:
|
if chunk:
|
||||||
yield chunk
|
yield chunk
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
# Timeout - check if process died
|
# Timeout - check if process died
|
||||||
if audio_process.poll() is not None:
|
if proc.poll() is not None:
|
||||||
break
|
break
|
||||||
except GeneratorExit:
|
except GeneratorExit:
|
||||||
pass
|
pass
|
||||||
|
|||||||
Reference in New Issue
Block a user