fix: close ffmpeg pipe handles when process has already terminated#2556
Open
tnoff wants to merge 1 commit intoZulko:masterfrom
Open
fix: close ffmpeg pipe handles when process has already terminated#2556tnoff wants to merge 1 commit intoZulko:masterfrom
tnoff wants to merge 1 commit intoZulko:masterfrom
Conversation
Both FFMPEG_AudioReader and FFMPEG_VideoReader open the ffmpeg subprocess
with stdout=PIPE and stderr=PIPE. The close() method in each class is
responsible for terminating the process and releasing those handles.
The bug: close() only called proc.stdout.close() / proc.stderr.close()
inside the `if self.proc.poll() is None:` branch — i.e., only when the
subprocess was still running at the time close() was called. When the
process had already exited on its own before close() was reached (the
common case when the caller exhausts the audio/video data), the handles
were left open and self.proc was set to None without closing them.
This caused Python to emit a ResourceWarning ("unclosed file") from the
garbage collector's finaliser (__del__ → close()), because by the time
the GC runs there is no longer any code path that will close the pipes.
The fix adds an `else` branch for the already-terminated case that closes
stdout and stderr unconditionally (with None guards for safety). The
running-process branch is unchanged: it still terminates, closes the
pipes, then waits — the correct order to avoid deadlocks when the child
is blocked writing to its stdout.
The same structural bug existed in both readers, so both are fixed here.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.
Fixes pytest bug warning
tests/