Skip to content

Use context manager for PIL Image.open to ensure proper resource cleanup#3000

Merged
adamtheturtle merged 1 commit intomainfrom
adamtheturtle/pil-image-context-manager
Feb 22, 2026
Merged

Use context manager for PIL Image.open to ensure proper resource cleanup#3000
adamtheturtle merged 1 commit intomainfrom
adamtheturtle/pil-image-context-manager

Conversation

@adamtheturtle
Copy link
Member

@adamtheturtle adamtheturtle commented Feb 22, 2026

Image.open() was used without explicit close or context manager. When the source is a file stream (e.g. multipart upload), this can hold file handles until garbage collection. Prefer 'with Image.open(...) as img:' to ensure files are closed promptly.

Fixes resource leak in:

  • image_matchers.py (StructuralSimilarityMatcher)
  • _query_validators/image_validators.py (validate_image_format, validate_image_dimensions, validate_image_is_image)
  • _services_validators/image_validators.py (validate_image_integrity, validate_image_format, validate_image_color_space, validate_image_is_image)
  • target.py (ImageTarget._post_processing_status)
  • target_raters.py (_get_brisque_target_tracking_rating)

Note

Low Risk
Mechanical resource-cleanup change (context managers around Image.open) with no functional logic changes expected beyond more deterministic file-handle release.

Overview
Ensures PIL.Image.open usages are wrapped in context managers across query validators, service validators, image matching, and target rating/status code paths so image/file handles are closed promptly.

This updates image dimension/format/is-image checks, integrity verification, SSIM matching, BRISQUE-based rating, and target post-processing status to use with Image.open(...) as ... (or as _) without changing validation thresholds or scoring logic.

Written by Cursor Bugbot for commit 59e100d. This will update automatically on new commits. Configure here.

Image.open() was used without explicit close or context manager. When
the source is a file stream (e.g. multipart upload), this can hold file
handles until garbage collection. Prefer 'with Image.open(...) as img:'
to ensure files are closed promptly.

Fixes resource leak in:
- image_matchers.py (StructuralSimilarityMatcher)
- _query_validators/image_validators.py (validate_image_format,
  validate_image_dimensions, validate_image_is_image)
- _services_validators/image_validators.py (validate_image_integrity,
  validate_image_format, validate_image_color_space, validate_image_is_image)
- target.py (ImageTarget._post_processing_status)
- target_raters.py (_get_brisque_target_tracking_rating)

Co-authored-by: Cursor <cursoragent@cursor.com>
@adamtheturtle adamtheturtle merged commit 4e76f60 into main Feb 22, 2026
12 checks passed
@adamtheturtle adamtheturtle deleted the adamtheturtle/pil-image-context-manager branch February 22, 2026 23:50
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