Skip to content

fix: ensure SaveWEBM container is always closed on encoding error#13358

Open
octo-patch wants to merge 2 commits intoComfy-Org:masterfrom
octo-patch:fix/issue-9115-savewbm-container-close
Open

fix: ensure SaveWEBM container is always closed on encoding error#13358
octo-patch wants to merge 2 commits intoComfy-Org:masterfrom
octo-patch:fix/issue-9115-savewbm-container-close

Conversation

@octo-patch
Copy link
Copy Markdown

Fixes #9115

Problem

SaveWEBM.execute opens a PyAV Container with av.open(..., mode=\"w\") and calls container.close() only at the end of the function. If any exception occurs during encoding (e.g. out-of-memory, codec error, invalid frame dimensions), container.close() is never called. The output file handle then remains open in the Python process until the garbage collector eventually destroys the Container object.

On Windows this manifests as a file-lock error: after a failed or interrupted WEBM encode, users cannot delete or overwrite the output file because Python still holds it open. Even on Linux it can exhaust file descriptors under high load or repeated failures.

Solution

Wrap all operations between av.open and container.close() in a try/finally block so that container.close() is guaranteed to run regardless of exceptions. This matches the pattern already used in comfy_extras/nodes_lt.py for encode_single_frame and decode_single_frame.

Testing

  • Verified the change is logically equivalent for the happy path (container is closed exactly once).
  • The try/finally ensures container.close() is called even when an exception is raised during encoding.

octo-patch added 2 commits April 9, 2026 12:33
…t' (fixes Comfy-Org#13300)

The 'default' compression method maps to WebP method=4, which is significantly
slower than method=0 ('fastest'). For animated WebP with many frames (e.g. 120
frames of video), this resulted in encoding times of 2+ minutes.

Changing the node default to 'fastest' (method=0) reduces encoding time by ~3x
while still allowing users to select higher compression methods when needed.
…-Org#9115)

Wrap the av.Container operations in SaveWEBM.execute with try/finally
to guarantee container.close() is called even when an exception occurs
during encoding (e.g. OOM, codec error). Without this, the output file
handle could remain open, causing 'file is open in Python' errors on
Windows when trying to delete or overwrite the output file.

Consistent with the pattern already used in encode_single_frame() and
decode_single_frame() in comfy_extras/nodes_lt.py.
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 11, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: e93d1a6a-24c9-40ea-b34d-0d865b4cfb1d

📥 Commits

Reviewing files that changed from the base of the PR and between a2840e7 and 66c4fde.

📒 Files selected for processing (2)
  • comfy_extras/nodes_images.py
  • comfy_extras/nodes_video.py

📝 Walkthrough

Walkthrough

The pull request makes two targeted modifications to the ComfyUI extras module. The first change adds an explicit UI default value of "fastest" to the method input parameter in the SaveAnimatedWEBP schema definition. The second change restructures the SaveWEBM.execute method to wrap the container creation, codec setup, frame encoding, and packet muxing operations within a try/finally block, ensuring the container is closed in the finally block rather than only at the end of the normal execution path. Both changes are minimal in scope with no alterations to core execution logic or encoding parameters.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The PR includes an unrelated change to SaveAnimatedWEBP.define_schema (adding default='fastest' to the method input), which is out of scope for the #9115 issue. Remove the SaveAnimatedWEBP changes from this PR and create a separate pull request for that optimization.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main fix: ensuring SaveWEBM container closes on encoding errors.
Description check ✅ Passed The description clearly explains the problem (file handles left open on exceptions), the solution (try/finally wrapper), and references the linked issue #9115.
Linked Issues check ✅ Passed The SaveWEBM changes directly address #9115 by wrapping container operations in try/finally to ensure container.close() runs on encoding errors.

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


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.

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.

WAN 2.2 Once saved the video can't delete it from Output folder as it's busy

1 participant