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.

ResourceWarning: unclosed file

See original GitHub issue

Problem description

I’m seeing a ResourceWarning: unclosed file warning when using context managers to open files/streams with smart_open.

Note: if I don’t run under unittest, or if I don’t gzip the file, resources seem to be closed correctly. I’m guessing unittest and smart_open are somehow not coordinating correctly on closing the layers.

Steps/code to reproduce the problem

My test script:

import unittest
from smart_open import open as smart_open


class MyTestCase(unittest.TestCase):
    def test_load(self):
        with smart_open('input.csv.gz') as fh:
            print("opened file")


unittest.main()

Invocation:

% echo -e 'col1,col2\nval1,val2\nval3,val4' | gzip > input.csv.gz 

% PYTHONTRACEMALLOC=1 python test_load.py
opened file
/Users/kwilliams/miniconda3/lib/python3.7/unittest/case.py:615: ResourceWarning: unclosed file <_io.BufferedReader name='input.csv.gz'>
  testMethod()
Object allocated at (most recent call last):
  File "/Users/kwilliams/git/dispatcher/rush-springs-simulations/venv/lib/python3.7/site-packages/smart_open/smart_open_lib.py", lineno 548
    fobj = io.open(parsed_uri.uri_path, mode)
.
----------------------------------------------------------------------
Ran 1 test in 0.007s

OK

Versions

Darwin-18.0.0-x86_64-i386-64bit
Python 3.7.3 (default, Mar 27 2019, 16:54:48) 
[Clang 4.0.1 (tags/RELEASE_401/final)]
smart_open 1.9.0

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:3
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
piskvorkycommented, Dec 4, 2019

@kenahoo thanks for the clear and detailed report. I agree context managers should be closing handles, so that looks like a bug.

@mpenkov is busy ATM – any chance you could take a stab at this yourself?

0reactions
mpenkovcommented, Aug 21, 2022

Cannot reproduce on linux Python 3.10.6 and smart_open 6.1.0.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python unclosed resource: is it safe to delete the file?
This ResourceWarning means that you opened a file, used it, but then forgot to close the file. Python closes it for you when...
Read more >
Fix "ResourceWarning: unclosed file" when running tests with ...
Running the tests highlights that we do not close file object explicitly in many places of the code. In Python 3.4+ this raises...
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 >
Issue 41395: pickle and pickletools cli interface doesn't close ...
python -Wall -m pickle mypickle {'a': 1} sys:1: ResourceWarning: unclosed file <_io.BufferedReader name='mypickle'> pickletools ...
Read more >
sys:1: ResourceWarning: unclosed <ssl.SSLSocket fd=4 ...
Bug 1733557 - sys:1: ResourceWarning: unclosed <ssl.SSLSocket fd=4, family=AddressFamily.AF_INET, type=SocketKind. ... ResourceWarning: unclosed file <_io.
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