-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Clean up encoders deterministically on success and failure #10043
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
danking
wants to merge
6
commits into
python-pillow:main
Choose a base branch
from
danking:fix-tiff-encoder-cleanup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a793e05
Clean up TIFF encoders before their output files close
danking b7844f5
Always clean up encoders used by Image.tobytes
danking d289f6f
Explain TIFF cleanup regression test
danking 568bf86
Fix release note heading underline
danking 09b6513
Subclass PyEncoder rather than using unittest
radarhere a30f2f6
Merge pull request #1 from radarhere/fix-tiff-encoder-cleanup
danking File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,52 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
|
|
||
| from PIL import Image, ImageFile | ||
|
|
||
| from .helper import hopper | ||
|
|
||
|
|
||
| def test_sanity() -> None: | ||
| data = hopper().tobytes() | ||
| assert isinstance(data, bytes) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("failure", (None, "setimage", "encode", "status")) | ||
| def test_encoder_cleanup(failure: str | None, monkeypatch: pytest.MonkeyPatch) -> None: | ||
| cleanup_called = False | ||
|
|
||
| class TestPyEncoder(ImageFile.PyEncoder): | ||
| def encode(self, bufsize: int) -> tuple[int, int, bytes]: | ||
| if failure == "encode": | ||
| raise ValueError(failure) | ||
| if failure == "status": | ||
| return (0, -2, b"") | ||
| return (6, 1, b"pixels") | ||
|
|
||
| def setimage( | ||
| self, | ||
| im: Image.core.ImagingCore, | ||
| extents: tuple[int, int, int, int] | None = None, | ||
| ) -> None: | ||
| if failure == "setimage": | ||
| raise ValueError(failure) | ||
| return super().setimage(im, extents) | ||
|
|
||
| def cleanup(self) -> None: | ||
| nonlocal cleanup_called | ||
| cleanup_called = True | ||
|
|
||
| im = Image.new("RGB", (1, 1)) | ||
|
|
||
| monkeypatch.setattr(Image, "ENCODERS", {"raw": TestPyEncoder}) | ||
| if failure in ("setimage", "encode"): | ||
| with pytest.raises(ValueError, match=failure): | ||
| im.tobytes() | ||
| elif failure == "status": | ||
| with pytest.raises(RuntimeError, match="encoder error -2 in tobytes"): | ||
| im.tobytes() | ||
| else: | ||
| assert im.tobytes() == b"pixels" | ||
|
|
||
| assert cleanup_called |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -954,6 +954,7 @@ ImagingLibTiffEncodeCleanup(ImagingCodecState state) { | |
| // that is fine, as it does not close the file | ||
| TIFFClose(tiff); | ||
| } | ||
| clientstate->tiff = NULL; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. 👍 |
||
| return 0; | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I can confirm that this test fails 100/100 runs before the fixes in this PR. The output file position is moved during the cleanup. It succeeds 100/100 runs with these changes.
I'm not sure if the bugs in the test suite are due to literally sharing a Python File object or if it was unlucky file descriptor reuse, but either way, I this test should pass, IMO.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I can tell, it's the latter.
The mechanism seems to be:
TiffImagePlugin._savedidn't dofinally:, so the encoder isn't explicitly cleaned up.ImagingLibTiffEncodeCleanupcallsTIFFClose/TIFFCleanup, which flush pending output into the now-unrelated file, clobbering it. 😞Apparently PyPy's GC is different enough (no refcounting, I understand?) that step 6 happens way further off "in other tests" in PyPy land, and with #9945 having landed, the test order is shuffled enough that this happens more obviously...