Skip to content

fix: guard temp-dir cleanup so image/dir input doesn't crash main()#420

Open
MushiSenpai wants to merge 1 commit into
TMElyralab:mainfrom
MushiSenpai:fix/save-dir-full-image-input
Open

fix: guard temp-dir cleanup so image/dir input doesn't crash main()#420
MushiSenpai wants to merge 1 commit into
TMElyralab:mainfrom
MushiSenpai:fix/save-dir-full-image-input

Conversation

@MushiSenpai

Copy link
Copy Markdown

Summary

scripts/inference.py crashes during cleanup when the input is an image or an
image directory (not a video).

save_dir_full is assigned only on the video-input branch:

if get_file_type(video_path) == "video":
    save_dir_full = os.path.join(temp_dir, input_basename)
    ...
elif get_file_type(video_path) == "image":   # save_dir_full never set
    ...
elif os.path.isdir(video_path):              # save_dir_full never set
    ...

…but cleanup calls it unconditionally:

shutil.rmtree(save_dir_full)   # UnboundLocalError for image / dir input

So for image or directory input, main() raises
UnboundLocalError: cannot access local variable 'save_dir_full' at cleanup. It's
swallowed by the surrounding except Exception, but only after the output video
is already written — so the user sees a spurious "Error occurred during processing:
…"
and the temp frame directory / coord file are left uncleaned.

The fix

Initialize save_dir_full = None before the input-type branch and guard the cleanup:

save_dir_full = None  # only set for video input
...
if save_dir_full is not None:
    shutil.rmtree(save_dir_full)

Two lines, no behavior change for video input.

Validation

The bug is pure control flow. Reproducing inference.py's exact structure
(save_dir_full bound only in the video branch, referenced at cleanup):

Input Before After
video cleanup runs cleanup runs (unchanged)
image UnboundLocalError cleanup skipped cleanly

Scope

Self-contained correctness fix, independent of #419 (the PyTorch 2.6 weights_only
face-parse fix). No existing issue tracked this; happy to open one if maintainers prefer.


Disclosure: this fix was drafted with AI assistance (Claude) and reviewed, tested, and
submitted by me. The commit carries a Co-Authored-By trailer.

scripts/inference.py assigned save_dir_full only on the video-input branch, but
cleanup called shutil.rmtree(save_dir_full) unconditionally. For image or
image-directory input the variable is never bound, raising UnboundLocalError at
cleanup (caught by the broad except, but only after the output is written, leaving
temp files uncleaned and printing a spurious "Error occurred during processing").
Initialize it to None and guard the rmtree.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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