Skip to content

fix: close ffmpeg pipe handles when process has already terminated#2556

Open
tnoff wants to merge 1 commit intoZulko:masterfrom
tnoff:warning
Open

fix: close ffmpeg pipe handles when process has already terminated#2556
tnoff wants to merge 1 commit intoZulko:masterfrom
tnoff:warning

Conversation

@tnoff
Copy link
Copy Markdown

@tnoff tnoff commented Feb 26, 2026

Fixes pytest bug warning

● Exception ignored in: <_io.FileIO name=3 mode='rb' closefd=True>                                                                                                                                                                                      
  Traceback (most recent call last):                                                                                                                                                                                                                    
    File "moviepy/audio/io/readers.py", line 309, in close                                                                                                                                                                                              
      self.proc = None                                                                                                                                                                                                                                  
  ResourceWarning: unclosed file <_io.BufferedReader name=3>                                                                                                                                                                                            
  Exception ignored in: <_io.FileIO name=5 mode='rb' closefd=True>                                                                                                                                                                                      
  Traceback (most recent call last):                                                                                                                                                                                                                    
    File "moviepy/audio/io/readers.py", line 309, in close                                                                                                                                                                                              
      self.proc = None                                                                                                                                                                                                                                  
  ResourceWarning: unclosed file <_io.BufferedReader name=5>  
  • I have provided code that clearly demonstrates the bug and that only works correctly when applying this fix
  • I have added suitable tests demonstrating a fixed bug or new/changed feature to the test suite in tests/
  • I have properly documented new or changed features in the documentation or in the docstrings
  • I have properly explained unusual or unexpected code in the comments around it

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>
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