From c5954503105d8def277b30b7037a925f18ac44bf Mon Sep 17 00:00:00 2001 From: Smittix Date: Thu, 15 Jan 2026 22:29:15 +0000 Subject: [PATCH] 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 --- routes/listening_post.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/routes/listening_post.py b/routes/listening_post.py index fb4960e..2e9cd83 100644 --- a/routes/listening_post.py +++ b/routes/listening_post.py @@ -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