fix(audio_generator): clean up temp file when gTTS save() fails - #346
Merged
msoedov merged 1 commit intoSep 22, 2026
Merged
Conversation
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
approved these changes
Sep 22, 2026
Owner
|
@jiawenlai1109 thx a lot for the patch! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
generate_audio_cross_platform()creates a temporary MP3 in the current workingdirectory and removes it in a
finallyblock — buttts.save()is called beforethe
try, so any exception raised by the save itself escapes the cleanup:gTTS.save()performs an outbound HTTP request, so this is exactly the path that failswhen the suite runs offline or behind egress restrictions — and the repo's
.gitignoredoes not cover
temp_audio_*, so every run leaves untrackedtemp_audio_<hex>.mp3files in the working tree.
generate_audio_mac_wav()immediately above already handles this correctly: itsfinallycleans up both temp paths with per-file error handling. This change makesgenerate_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:
temp_audio_*.mp3left in CWDgTTSError: Failed to connect)gTTSError)black --checkon 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_platformdepends on outbound network, so it fails in anyoffline sandbox. I left it alone deliberately — happy to add an offline guard in a
separate commit if you want that behaviour decided.