Skip to content

fix(audio_generator): clean up temp file when gTTS save() fails - #346

Merged
msoedov merged 1 commit into
msoedov:mainfrom
jiawenlai1109:fix/audio-temp-leak-on-save-error
Sep 22, 2026
Merged

msoedov merged 1 commit into
msoedov:mainfrom
jiawenlai1109:fix/audio-temp-leak-on-save-error

Conversation

@jiawenlai1109

Copy link
Copy Markdown
Contributor

Problem

generate_audio_cross_platform() creates a temporary MP3 in the current working
directory and removes it in a finally block — but tts.save() is called before
the try, so any exception raised by the save itself escapes the cleanup:

temp_mp3_path = f"temp_audio_{uuid.uuid4().hex}.mp3"
tts.save(temp_mp3_path)          # file is already on disk here

try:
    with open(temp_mp3_path, "rb") as f:
        audio_bytes = f.read()
finally:
    if os.path.exists(temp_mp3_path):
        os.remove(temp_mp3_path)

gTTS.save() performs an outbound HTTP request, so this is exactly the path that fails
when the suite runs offline or behind egress restrictions — and the repo's .gitignore
does not cover temp_audio_*, so every run leaves untracked temp_audio_<hex>.mp3
files in the working tree.

generate_audio_mac_wav() immediately above already handles this correctly: its
finally cleans up both temp paths with per-file error handling. This change makes
generate_audio_cross_platform() consistent with the pattern already used in this file.

Change

One statement moved inside the existing try. No behaviour change on the success path.

     tts = gTTS(text=prompt, lang="en")
     temp_mp3_path = f"temp_audio_{uuid.uuid4().hex}.mp3"
-    tts.save(temp_mp3_path)
-
     try:
+        tts.save(temp_mp3_path)
         with open(temp_mp3_path, "rb") as f:
             audio_bytes = f.read()
     finally:

Evidence

Ubuntu 22.04, Python 3.14.7, test run with no route to the Google TTS endpoint:

run test result temp_audio_*.mp3 left in CWD
before 1 failed (gTTSError: Failed to connect) 1
after 1 failed (same gTTSError) 0

black --check on the file: unchanged. Rest of the module: 1 passed, 1 skipped, 1 failed
(the failure is the pre-existing offline one, identical before and after).

Not changed in this PR

test_generate_audio_cross_platform depends on outbound network, so it fails in any
offline sandbox. I left it alone deliberately — happy to add an offline guard in a
separate commit if you want that behaviour decided.


---

generate_audio_cross_platform() created the temp MP3 via tts.save() one
statement *before* the try/finally that was supposed to remove it, so any
exception raised by the save itself escaped the cleanup and left
temp_audio_<hex>.mp3 in the current working directory.

gTTS.save() performs an outbound HTTP request, which is exactly the path that
fails offline or behind egress restrictions, and .gitignore does not cover the
temp_audio_* pattern - so every such run litters the working tree.

Move the save inside the existing try. Success-path behaviour is unchanged, and
this now matches how generate_audio_mac_wav() in the same file already guards
its two temp paths.

Verified on Ubuntu 22.04 / Python 3.14.7, same offline-failing test before and
after: leaked temp_audio_*.mp3 count went 1 -> 0, test result unchanged
(1 failed, gTTSError: Failed to connect). black --check passes; rest of the
module is 1 passed, 1 skipped, 1 pre-existing offline failure.
@msoedov

msoedov commented Sep 22, 2026

Copy link
Copy Markdown
Owner

@jiawenlai1109 thx a lot for the patch!

@msoedov
msoedov merged commit bdf2208 into msoedov:main Sep 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants