-
Notifications
You must be signed in to change notification settings - Fork 108
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
ResourceWarning: unclosed file #97
Comments
Can you provide any way to reproduce this problem? With just a context-free warning message like this, it's impossible to tell what's going on. |
hello! I wrote the lines to reproduce this problem: import unittest
import librosa
class Bar():
def __getitem__(self, idx):
wav = librosa.load('~/Music/test.mp3') # or whatever audio file
return {'wav': wav}
class Foo(unittest.TestCase):
def test_bar(self):
b = Bar()
y = b[0]['wav']
print(y) save the code in a file(
And i found if i directly call the class method, this warning won't pop up |
Hmm… I notice you're using the library via librosa, not directly. It seems like there's a strong chance this could be a bug in librosa itself, not in audioread, if it's not letting the file close properly. Maybe it would be worth digging to see if you can reproduce this when using audioread directly? |
hello! I used audioread directly as follows: import unittest
import os
import audioread
class Bar():
def __getitem__(self, idx):
y = []
with audioread.audio_open(os.path.realpath('ANY_AUDIO.mp3')) as input_file:
for frame in input_file:
y.append(frame)
return {'wav': y}
class Foo(unittest.TestCase):
def test_bar(self):
b = Bar()
print('before read')
y = b[0]['wav']
print('read')
# f = Foo()
# f.test_bar() run it as mentioned before, the warnings pop up the same way. |
Thanks for narrowing that down! It's odd, though—I ran your code verbatim, and it runs without a warning:
This is Python 3.7.3 on macOS 10.14.5. I'm using the latest git master source of audioread—could you be using an older version? |
I use Python 3.6.5 and audioread 2.1.8 on Ubuntu 18.04. Slightly changed code as in the attached file (to keep comment short&clean) Then i found by turning warnings into exceptions the code would have same behavior: $ python -W error test.py
before read
Exception ignored in: <_io.FileIO name=4 mode='rb' closefd=True>
ResourceWarning: unclosed file <_io.BufferedReader name=4>
Exception ignored in: <_io.FileIO name=6 mode='rb' closefd=True>
ResourceWarning: unclosed file <_io.BufferedReader name=6>
read
$ python -W error -m unittest test.Foo.test_bar
before read
Exception ignored in: <_io.FileIO name=4 mode='rb' closefd=True>
ResourceWarning: unclosed file <_io.BufferedReader name=4>
Exception ignored in: <_io.FileIO name=6 mode='rb' closefd=True>
ResourceWarning: unclosed file <_io.BufferedReader name=6>
read
before read
Exception ignored in: <_io.FileIO name=4 mode='rb' closefd=True>
ResourceWarning: unclosed file <_io.BufferedReader name=4>
Exception ignored in: <_io.FileIO name=6 mode='rb' closefd=True>
ResourceWarning: unclosed file <_io.BufferedReader name=6>
read
.
----------------------------------------------------------------------
Ran 1 test in 0.077s
OK
$ python -m unittest test.Foo.test_bar
before read
read
before read
/home/beantowel/anaconda3/lib/python3.6/site-packages/audioread/__init__.py:86: ResourceWarning: unclosed file <_io.BufferedReader name=4>
if ffdec.available():
/home/beantowel/anaconda3/lib/python3.6/site-packages/audioread/__init__.py:86: ResourceWarning: unclosed file <_io.BufferedReader name=6>
if ffdec.available():
read
.
----------------------------------------------------------------------
Ran 1 test in 0.079s
OK |
Got it; thanks! I was actually able to reproduce this by forcing my system to use the FFmpeg backend. But these things are actually super hard to debug, and it will take some work to narrow it down. Any chance you'd be interested in helping out? We'll need to audit all the file handles opened in |
(Running into this issue myself from running |
Simplified the reproducer to just:
Adding
|
Hmm; thanks for investigation! Unfortunately this output doesn't quite pin it down… it's pointing to the FWIW, here is where we should be closing the stdout and stderr streams: Lines 307 to 308 in 8d02710
|
After upgrading my pip packages(i don't know which cause it), running programs previously having no warnings pop up this message frequently. How to fix it?
The text was updated successfully, but these errors were encountered: