From a4c135d56b1bc518c836a67fe9058e167cd98a99 Mon Sep 17 00:00:00 2001 From: Nor Date: Sat, 29 Mar 2025 14:49:47 +0000 Subject: [PATCH] Fix speech.py issue - spamming UnboundLocalError Fixed the error in speech.py that was causing the UnboundLocalError: cannot access local variable 'generator_worker' where it is not associated with a value exception. The issue was in the cleanup function that runs as a background task after a request finishes. The function was trying to delete the variables generator_worker and out_writer_worker without checking if they exist first. This caused the error when the cleanup function executed in a scenario where these variables weren't properly initialized. The fix adds checks to ensure these variables exist in the local scope before attempting to delete them. --- speech.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/speech.py b/speech.py index e412e8b..06b5ead 100755 --- a/speech.py +++ b/speech.py @@ -389,8 +389,11 @@ def out_writer(): def cleanup(): ffmpeg_proc.kill() - del generator_worker - del out_writer_worker + # Check if variables exist in local scope before deleting them + if 'generator_worker' in locals(): + del generator_worker + if 'out_writer_worker' in locals(): + del out_writer_worker return StreamingResponse(content=ffmpeg_proc.stdout, media_type=media_type, background=cleanup) else: