Skip to content

fix: use try/finally for Bilibili temp file cleanup in merge_bilibili_video_audio#725

Open
Arvuno wants to merge 1 commit into
Evil0ctal:mainfrom
Arvuno:contrib/douyin/config-parsing-edge-cases
Open

fix: use try/finally for Bilibili temp file cleanup in merge_bilibili_video_audio#725
Arvuno wants to merge 1 commit into
Evil0ctal:mainfrom
Arvuno:contrib/douyin/config-parsing-edge-cases

Conversation

@Arvuno
Copy link
Copy Markdown

@Arvuno Arvuno commented May 21, 2026

Summary

  • Refactor to use a block instead of for temp file cleanup
  • Initialize and to before the try block so the clause can safely clean them up even if creation fails
  • Convert bare to to catch only file-not-found errors

Problem

The original code has a temp file leak: and are assigned inside the statements but declared only within the block. If throws an exception before assignment (e.g., disk full, permission error), the variables are unbound and the cleanup code crashes. Additionally, bare is a Python anti-pattern.

Solution

  • Move variable declarations before with sentinel
  • Use pattern (not ) so cleanup always runs regardless of how the function exits
  • Guard cleanup with to skip cleanup for
  • Replace bare with since only raises (subclass of OSError) when the file doesn't exist

Tests

No existing tests for this function — manual verification:

  • creates both paths → finally cleans both
  • If throws before assignment → variables stay → finally skips cleanup (no NameError)
  • If ffmpeg fails → return → finally still runs → temp files cleaned

Risk / Compatibility

  • Risk: LOW — behavior change is strictly correct (no functional difference for the success case; the bug fix prevents file leaks on error paths)
  • Affects: runtime behavior (error path temp file cleanup only)
  • No public API changes

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