From 4576afac39b0e8721b0c20094b10519e39c92ee8 Mon Sep 17 00:00:00 2001 From: Russell Ballestrini Date: Mon, 10 Nov 2025 04:01:39 -0500 Subject: [PATCH] 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. --- speech.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/speech.py b/speech.py index 6e6d358..96de76a 100755 --- a/speech.py +++ b/speech.py @@ -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