Skip to content

fix: close mask image file handle in /upload/mask endpoint#13336

Open
mango766 wants to merge 1 commit intoComfy-Org:masterfrom
mango766:fix/mask-upload-unclosed-file-handle
Open

fix: close mask image file handle in /upload/mask endpoint#13336
mango766 wants to merge 1 commit intoComfy-Org:masterfrom
mango766:fix/mask-upload-unclosed-file-handle

Conversation

@mango766
Copy link
Copy Markdown

@mango766 mango766 commented Apr 9, 2026

Fixes #13335

What

In the /upload/mask handler, the uploaded mask image was opened without a context manager:

# before
mask_pil = Image.open(image.file).convert('RGBA')

Image.open() returns a lazy-loaded image that keeps the underlying file descriptor open. Calling .convert() produces a new in-memory copy but does not close the original. Without an explicit close the file handle leaks — it's only reclaimed when CPython's refcount drops to zero (i.e. next GC cycle), which is non-deterministic.

The original image a couple lines above was already using a context manager correctly; this PR brings the mask open in line with that pattern:

# after
with Image.open(image.file) as mask_pil_raw:
    mask_pil = mask_pil_raw.convert('RGBA')

Why it matters

Under sustained mask-upload load the leaked descriptors can push the process past the OS limit (ulimit -n, default 1024 on most Linux distros), causing "Too many open files" errors.

Change

  • server.py: wrap Image.open(image.file) in a with block (1-line net change)

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 9, 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: 5f92e751-3cf2-4e3a-82ee-26a112126a5f

📥 Commits

Reviewing files that changed from the base of the PR and between 2d861fb and 4b97d16.

📒 Files selected for processing (1)
  • server.py

📝 Walkthrough

Walkthrough

The upload_mask function in server.py has been modified to change how the mask image is loaded. The image file opening now uses a context manager pattern to ensure proper resource cleanup. The PIL image is opened within a context manager, converted to RGBA format, and the file handle is closed after the conversion. The subsequent image manipulation logic remains unchanged.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
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 (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the primary fix: closing a mask image file handle in the /upload/mask endpoint.
Description check ✅ Passed The description is well-related to the changeset, explaining the issue, the fix, and why it matters with clear before/after code examples.
Linked Issues check ✅ Passed The PR directly addresses issue #13335 by implementing the exact fix specified: wrapping Image.open(image.file) in a context manager to ensure proper file handle closure.
Out of Scope Changes check ✅ Passed All changes are narrowly scoped to the specific file descriptor leak fix in the /upload/mask endpoint with no unrelated modifications.

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

Warning

Review ran into problems

🔥 Problems

Git: Failed to clone repository. Please run the @coderabbitai full review command to re-trigger a full review. If the issue persists, set path_filters to include or exclude specific files.


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.

Unclosed file handle when loading mask image in /upload/mask endpoint

1 participant