Add detailed audio stream generator logging

- Log when generator starts
- Track iterations and bytes sent
- Log select timeouts to diagnose data flow issues

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Smittix
2026-01-15 16:48:51 +00:00
parent 52f85669f8
commit b27a532bce
+8 -2
View File
@@ -904,24 +904,30 @@ def stream_audio() -> Response:
def generate(): def generate():
bytes_sent = 0 bytes_sent = 0
iterations = 0
logger.info("Audio stream generator started")
try: try:
while audio_running and audio_process and audio_process.poll() is None: while audio_running and audio_process and audio_process.poll() is None:
iterations += 1
# Use select to avoid blocking forever # Use select to avoid blocking forever
ready, _, _ = select.select([audio_process.stdout], [], [], 2.0) ready, _, _ = select.select([audio_process.stdout], [], [], 2.0)
if ready: if ready:
chunk = audio_process.stdout.read(4096) chunk = audio_process.stdout.read(4096)
if chunk: if chunk:
bytes_sent += len(chunk) bytes_sent += len(chunk)
if bytes_sent < 50000 or bytes_sent % 100000 < 4096:
logger.debug(f"Audio chunk: {len(chunk)} bytes, total: {bytes_sent}")
yield chunk yield chunk
else: else:
logger.warning(f"Audio stream: empty chunk after {bytes_sent} bytes") logger.warning(f"Audio stream: empty chunk after {bytes_sent} bytes, iterations={iterations}")
break break
else: else:
logger.warning(f"Audio stream: select timeout after {bytes_sent} bytes, iterations={iterations}, poll={audio_process.poll()}")
# Timeout - check if process died # Timeout - check if process died
if audio_process.poll() is not None: if audio_process.poll() is not None:
logger.warning(f"Audio process died during streaming after {bytes_sent} bytes") logger.warning(f"Audio process died during streaming after {bytes_sent} bytes")
break break
logger.info(f"Audio stream ended - sent {bytes_sent} bytes, running={audio_running}, poll={audio_process.poll() if audio_process else 'N/A'}") logger.info(f"Audio stream ended - sent {bytes_sent} bytes, iterations={iterations}, running={audio_running}, poll={audio_process.poll() if audio_process else 'N/A'}")
except GeneratorExit: except GeneratorExit:
logger.info(f"Audio stream: client disconnected after {bytes_sent} bytes") logger.info(f"Audio stream: client disconnected after {bytes_sent} bytes")
except Exception as e: except Exception as e: