Skip to content

Update audio.py#45

Open
subhshrivastava29918 wants to merge 1 commit into
suitenumerique:mainfrom
subhshrivastava29918:patch-1
Open

Update audio.py#45
subhshrivastava29918 wants to merge 1 commit into
suitenumerique:mainfrom
subhshrivastava29918:patch-1

Conversation

@subhshrivastava29918
Copy link
Copy Markdown

The temp file is written, then whisperx.load_audio() is called, then os.remove() is called. If load_audio raises an exception the remove call is never reached, leaking the file on disk. Any crash, unsupported format, or corrupt audio leaves a dangling temp file.

In audio.py, the endpoint writes a temp file, then calls whisperx.load_audio(), then deletes the file. If load_audio throws — due to a corrupt upload, unsupported codec, or an OOM error — the os.remove() call is never reached and the file sits on disk permanently. The fix is a try/finally block so deletion is guaranteed regardless of what happens

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 26, 2026

Walkthrough

The audio endpoint implementation was modified to wrap the audio loading operation in a finally block that ensures the temporary uploaded file is deleted regardless of whether the operation succeeds or raises an exception. The transcription logic and response schema remain unchanged, with the change focused purely on resource cleanup.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Title check ❓ Inconclusive The title 'Update audio.py' is vague and generic, using non-descriptive terms that don't convey meaningful information about the specific change (resource cleanup via try/finally). Consider a more descriptive title such as 'Ensure temp audio files are always deleted via try/finally block' to clearly indicate the primary change.
✅ Passed checks (4 passed)
Check name Status Explanation
Description check ✅ Passed The description clearly explains the problem (temp file leakage on exceptions) and the solution (try/finally block), making it directly related to the changeset.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@sonarqubecloud
Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
B Maintainability Rating on New Code (required ≥ A)
E Reliability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
app/endpoints/audio.py (1)

78-85: ⚠️ Potential issue | 🔴 Critical

Critical: indentation breaks the module — try/finally (and the lines below) are at column 0, outside the async def.

The new try: / finally: block, as well as result = transcribe(...) and return AudioTranscription(**result), are written at the top level (column 0) instead of being indented inside audio_transcriptions. Python will raise an IndentationError at import time, so the endpoint — and likely the whole app — will fail to start. This needs to be fixed before merging.

🛠️ Proposed fix
     logger.info("Loading audio file to whisper…")
-try:
-    audio = whisperx.load_audio(temp_file_path)
-finally:
-    os.remove(temp_file_path)
-
-    result = transcribe(audio, settings, language)
-
-    return AudioTranscription(**result)
+    try:
+        audio = whisperx.load_audio(temp_file_path)
+    finally:
+        os.remove(temp_file_path)
+
+    result = transcribe(audio, settings, language)
+
+    return AudioTranscription(**result)

Note that result = transcribe(...) and return AudioTranscription(...) should remain outside the finally block (so cleanup runs before transcription, but transcription still happens on the loaded audio value).

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@app/endpoints/audio.py` around lines 78 - 85, The try/finally and subsequent
lines were dedented to column 0 and must be moved inside the async endpoint
function audio_transcriptions; indent the try: block (calling
whisperx.load_audio(temp_file_path)) and the finally: block (calling
os.remove(temp_file_path)) so they execute within audio_transcriptions, ensure
the variable audio is defined in the function scope before the finally, and keep
the lines result = transcribe(audio, settings, language) and return
AudioTranscription(**result) outside the finally block (so cleanup runs first
but transcription and return still use the loaded audio).
🧹 Nitpick comments (1)
app/endpoints/audio.py (1)

80-81: Minor: guard os.remove against a missing file and log cleanup failures.

If whisperx.load_audio somehow deletes/moves the temp file, or the file was never fully created, os.remove(temp_file_path) inside finally will raise and mask the original exception from the try block. Consider os.path.exists check or swallowing/logging the cleanup error so the real error surfaces to the caller.

♻️ Suggested hardening
     try:
         audio = whisperx.load_audio(temp_file_path)
     finally:
-        os.remove(temp_file_path)
+        try:
+            os.remove(temp_file_path)
+        except OSError as cleanup_err:
+            logger.warning("Failed to remove temp file %s: %s", temp_file_path, cleanup_err)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@app/endpoints/audio.py` around lines 80 - 81, The finally block currently
unconditionally calls os.remove(temp_file_path) which can raise and mask earlier
exceptions (e.g., after whisperx.load_audio moved/deleted the file); update the
cleanup to guard removal by checking os.path.exists(temp_file_path) or wrapping
os.remove(...) in its own try/except that logs cleanup failures (use the module
logger or processLogger) and suppresses the removal error so the original
exception from the try block still surfaces; reference the temp_file_path
variable and the finally block around whisperx.load_audio to locate the change.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@app/endpoints/audio.py`:
- Around line 78-85: The try/finally and subsequent lines were dedented to
column 0 and must be moved inside the async endpoint function
audio_transcriptions; indent the try: block (calling
whisperx.load_audio(temp_file_path)) and the finally: block (calling
os.remove(temp_file_path)) so they execute within audio_transcriptions, ensure
the variable audio is defined in the function scope before the finally, and keep
the lines result = transcribe(audio, settings, language) and return
AudioTranscription(**result) outside the finally block (so cleanup runs first
but transcription and return still use the loaded audio).

---

Nitpick comments:
In `@app/endpoints/audio.py`:
- Around line 80-81: The finally block currently unconditionally calls
os.remove(temp_file_path) which can raise and mask earlier exceptions (e.g.,
after whisperx.load_audio moved/deleted the file); update the cleanup to guard
removal by checking os.path.exists(temp_file_path) or wrapping os.remove(...) in
its own try/except that logs cleanup failures (use the module logger or
processLogger) and suppresses the removal error so the original exception from
the try block still surfaces; reference the temp_file_path variable and the
finally block around whisperx.load_audio to locate the change.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 3a8fa206-1f1b-4ad6-bc27-82aa11abf2a1

📥 Commits

Reviewing files that changed from the base of the PR and between 320130b and b6d4617.

📒 Files selected for processing (1)
  • app/endpoints/audio.py

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.

1 participant