question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ffmpeg.Reader._get_cam_inputname causes "ResourceWarning: unclosed file"

See original GitHub issue

Description

In my tests I keep seeing ResourceWarning: unclosed file <_io.BufferedWriter name=3>

With PYTHONTRACEMALLOC=1, here’s what I get:

...\lib\site-packages\imageio\plugins\ffmpeg.py:303: ResourceWarning: unclosed file <_io.BufferedWriter name=3>
  self._filename = self._get_cam_inputname(index)
Object allocated at (most recent call last):
  File "...\lib\subprocess.py", lineno 935
    self.stdin = io.open(p2cwrite, 'wb', bufsize)
...\lib\site-packages\imageio\plugins\ffmpeg.py:303: ResourceWarning: unclosed file <_io.BufferedReader name=4>
  self._filename = self._get_cam_inputname(index)
Object allocated at (most recent call last):
  File "...\lib\subprocess.py", lineno 941
    self.stdout = io.open(c2pread, 'rb', bufsize)
...\lib\site-packages\imageio\plugins\ffmpeg.py:303: ResourceWarning: unclosed file <_io.BufferedReader name=5>
  self._filename = self._get_cam_inputname(index)
Object allocated at (most recent call last):
  File "...\lib\subprocess.py", lineno 946
    self.stderr = io.open(errread, 'rb', bufsize)

How to reproduce

On a Windows 10 laptop with built-in webcam (<video0>), using Python 3.9.7, imageio 2.11.0, imageio-ffmpeg 0.4.5, run the following test:

from imageio import get_reader
import unittest

class ReaderTests(unittest.TestCase):
    def test_get_reader(self):
        reader = get_reader(uri='<video0>')

Cause

Looking at the source of _get_cam_inputname, and looking at the trace above, I suspect the issue is caused by the stdin, stdout, and stderr streams not being closed properly after use in subprocess.Popen.

Proposed solution

Use subprocess.Popen as a context manager, to ensure streams are always properly closed, as suggested e.g. here.

Replace this (source):

...
proc = sp.Popen(
    cmd, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE, shell=True
)
proc.stdout.readline()
proc.terminate()
infos = proc.stderr.read().decode("utf-8", errors="ignore")
...

by this:

...
with sp.Popen(
         cmd, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE,
         shell=True) as proc:
    proc.stdout.readline()
    proc.terminate()
    infos = proc.stderr.read().decode("utf-8", errors="ignore")
...

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
dennisvangcommented, Dec 17, 2021

@FirefoxMetzger Sorry it took so long, I had some other, more pressing, concerns.

I tried to address the issue both here and in imageio-ffmpeg, see above.

0reactions
dennisvangcommented, Dec 1, 2021

@FirefoxMetzger It looks like you are right. I’ll give it a try.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ResourceWarning: unclosed file <_io.BufferedReader name=4>
What is the actual cause of the warning and how to fix it? Note that if I comment out unittest.main() the problem disappears,...
Read more >
ResourceWarning: unclosed file (for Python 3) · Issue #477
A single line img=Image.open(filename) causes a warning in Python 3.3: ResourceWarning: unclosed file <_io.BufferedReader name='.
Read more >
Adding Images triggers "ResourceWarning: unclosed file"
Adding Images triggers "ResourceWarning: unclosed file". Doing a basic test with Warnings enabled. import openpyxl wb = openpyxl.
Read more >
Python console: Error processing when file is saved however ...
... I get the following error message: warning::1: resourcewarning: unclosed file. To run this line of script I have the file open in...
Read more >
ResourceWarning for unclosed files in ssh backend
Using the duplicity 0.8.04 ssh backend on Debian unstable (Python 3.7.5rc1), I get ResourceWarning messages about unclosed files.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found