fix: prevent temp file leak and race condition in save_formatted_audio#1910
Merged
romanlutz merged 8 commits intoJun 4, 2026
Merged
Conversation
- Use tempfile.NamedTemporaryFile instead of fixed temp_audio.wav - Wrap Azure upload in try/finally so temp file is always deleted - Add import for tempfile module Fixes microsoft#1894
18c5f9f to
7e6e67d
Compare
romanlutz
requested changes
Jun 4, 2026
Contributor
romanlutz
left a comment
There was a problem hiding this comment.
Thanks for tackling this — the direction is right (real race + real leak), but the PR is incomplete and ships a regression in test coverage. Details inline.
Summary:
- 🔴 The existing regression test
test_save_formatted_audio_azure_storage_unlinks_local_temp(tests/unit/models/test_data_type_serializer.py:619) is now silently a no-op against this change — verified locally that it still passes even if you delete the entirefinallyblock. - 🔴 PR description claims "Added regression test that mocks Azure upload to raise" but the diff contains zero test changes. That exception-path test is the most important piece of this fix and should land here.
- 🔴 Trailing whitespace on line 235 will fail the
trailing-whitespacepre-commit hook. - 🟡 Unmentioned behavioral change: the
if self._memory.results_storage_io is None:check moved out of theasync with aiofiles.open(...)block. It's actually a small improvement (file handle closes before remote upload), but worth calling out.
No async-suffix or keyword-only violations. delete=False is correct here since wave.open() re-opens the path.
- Fix existing test assertion to use glob pattern instead of fixed filename - Add exception-path regression test for upload failure cleanup - Remove trailing whitespace
Contributor
Author
|
Hi @romanlutz
|
romanlutz
reviewed
Jun 4, 2026
Contributor
romanlutz
left a comment
There was a problem hiding this comment.
Thanks for the quick turnaround! All three blocking items from the previous review are resolved:
- ✅ Trailing whitespace removed (data_type_serializer.py:235).
- ✅ Existing
test_save_formatted_audio_azure_storage_unlinks_local_tempupdated toassert list(tmp_path.glob("*.wav")) == []. I verified locally that it now genuinely fails if thefinallyblock is removed (previously a tautology under the new naming). - ✅ New exception-path test
test_save_formatted_audio_async_cleans_up_temp_file_on_azure_upload_failureadded. I verified locally that it also fails if thefinallyblock is removed — so it's a real regression guard, not just coverage.
One small CI blocker left (E302) — inline. Once that's fixed I'm happy to approve.
Minor nits (non-blocking, your call):
@pytest.mark.asyncioon the new test is redundant —asyncio_mode=autois set inpyproject.tomland no other test in this file uses the decorator. Dropping it would match the file's pattern.existing_wav_files = set(tmp_path.glob("*.wav"))is defensive but unnecessary sincetmp_pathis a fresh per-test pytest fixture;assert list(tmp_path.glob("*.wav")) == []would be consistent with the sibling test.
Contributor
Author
|
Hi @romanlutz
|
…n test Ruff reported E302 because @pytest.mark.asyncio sat one blank line below the prior test. Add the second blank line to satisfy the rule. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
romanlutz
approved these changes
Jun 4, 2026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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
DataTypeSerializer.save_formatted_audiohad two bugs in the Azure storage branch:File leak:
os.remove(local_temp_path)was not in afinallyblock,so if the Azure upload raised an exception,
temp_audio.wavwas never deleted.Race condition: The temp file always used the fixed name
temp_audio.wav,so concurrent calls would clobber each other's WAV before upload.
Fix
temp_audio.wavwithtempfile.NamedTemporaryFileto giveeach call a unique path
try/finallyso the temp file is always deletedTests
no new
.wavfiles remain inDB_DATA_PATHFixes #1894