Fix UnboundLocalError in cleanup function

The cleanup() callback was trying to delete generator_worker and
out_writer_worker unconditionally, but these variables are only
defined in certain code paths. This caused UnboundLocalError when
cleanup was called after requests that didn't create these workers.

Wrap the deletions in try/except blocks to handle cases where the
variables weren't created.
This commit is contained in:
Russell Ballestrini 2025-11-10 04:01:39 -05:00
parent bb9823f6d0
commit 4576afac39

View file

@ -734,8 +734,15 @@ async def generate_speech(request: GenerateSpeechRequest):
def cleanup():
ffmpeg_proc.kill()
del generator_worker
del out_writer_worker
# Only delete workers if they were created
try:
del generator_worker
except NameError:
pass
try:
del out_writer_worker
except NameError:
pass
return StreamingResponse(content=ffmpeg_proc.stdout, media_type=media_type, background=cleanup)
# Use Silero for tts-1-silero