Skip to content

Clear TIFF core image if memory mapping was used for last load #8962

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
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

radarhere
Copy link
Member

@radarhere radarhere commented May 21, 2025

Resolves #8961

Using memory mapping to load one frame, and then not using it for the next frame, is causing a problem. This would be because memory mapping is replacing the core image.

Pillow/src/PIL/ImageFile.py

Lines 324 to 326 in 3c71559

self.im = Image.core.map_buffer(
self.map, self.size, decoder_name, offset, args
)

But TiffImagePlugin tries to keep using it afterwards.

def seek(self, frame: int) -> None:
"""Select a given frame as current image"""
if not self._seek_check(frame):
return
self._seek(frame)
if self._im is not None and (
self.im.size != self._tile_size or self.im.mode != self.mode
):
# The core image will no longer be used
self._im = None

@wiredfool
Copy link
Member

Does it make sense to set the c level read-only flag on the image if it's memmapped, and then check if the image is read-only instead? Would the mmap also cause an issue if someone were to try to imagedraw on it?

@radarhere
Copy link
Member Author

I've updated the commit to use the Python readonly flag.

ImageDraw should be fine, thanks to this code in ImageDraw.__init__.

if im.readonly:
im._copy() # make it writeable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Segfault when seeking to position 0 from any position in a TIFF image and reading the numpy array
2 participants