close GIF file handle after frame extraction in load_video#13854
Open
gagandhakrey wants to merge 2 commits into
Open
close GIF file handle after frame extraction in load_video#13854gagandhakrey wants to merge 2 commits into
gagandhakrey wants to merge 2 commits into
Conversation
Signed-off-by: Gagan Dhakrey <gagandhakrey@gmail.com>
Contributor
Author
|
Hi @sayakpaul, could you please take a quick look at this small GIF file handle fix ? thanks |
Contributor
Author
|
Bumping it again |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fix: Close GIF File Handle in
load_videoProblem
In
load_video,PIL.Image.open()is called on a GIF file, but the underlying file handle is never explicitly closed after all frames are extracted.Impact
PIL.Image.open()keeps the underlying OS file descriptor open for the lifetime of the image object. Without an explicitclose(), the descriptor is only released when Python's garbage collector destroys the object, which is non-deterministic.Under repeated calls to
load_videowith GIF inputs, file descriptors can accumulate and eventually exhaust the process limit, resulting in:Fix
Wrap
PIL.Image.open()in a context manager so the file handle is deterministically closed once all frames have been copied.This change is safe because
gif.copy()creates independent in-memoryPIL.Imageobjects. Once the frames are copied, the underlying GIF file can be closed without affecting the returned images.