-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: read_csv can leak file handles if validation fails #45384
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
Comments
The issue is with exceptions raised during the initialisation of e.g. But a few lines later there is another fallible call which isn't checked (the one which causes the issue in the repro): It looks like this would be fairly easy to fix - everything after |
@twoertwein can you reproduce this? if so, can you check if a regression or long standing bug. |
I can see that the file handle is leaked (but I cannot reproduce the unlink error since I don't have access to windows). Python disables ResourceWarnings by default, but running the example with It is very likely a bug introduced in 1.2 (centralized most of the IO in get_handle). |
@mrob95 if you need a quick workaround, the following works on linux (probably also on windows): import os
import pandas as pd
TEST_CONTENTS = """
col1,col2
a,b
1,2
"""
TEST_PATH = "file_leak_example.csv"
with open(TEST_PATH, "w+") as f:
f.write(TEST_CONTENTS)
f.seek(0)
try:
pd.read_csv(f, usecols=["col1", "col2", "col3"], engine="c")
except:
f.close()
os.unlink(TEST_PATH) |
@simonjayhawkins is that something that should be fixed before the very soon release of 1.4? |
Thanks, I shipped this exact fix last week but only just got around to filing it :) |
not necessarily for 1.4.0, but bug fixes and regression fixes can be backported to 1.4.x so if a regression should label as such so that the committer/reviewer can make a better judgment about backport. |
Pandas version checks
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
NOTE: I think this will only repro the issue on Windows, as other OSs do not prevent you deleting a file that is still open.
On Windows, the above repro causes the following:
The
ValueError
is correct, but theos.unlink
should not fail. The reason it does is due to the csv parser holding onto the file handle.Expected Behavior
If an exception is encountered during file reading all handles should be closed, allowing invalid files to be moved or deleted.
Installed Versions
In [2]: pd.show_versions()
INSTALLED VERSIONS
commit : ad19057
python : 3.9.5.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19043
machine : AMD64
processor : Intel64 Family 6 Model 60 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : English_United Kingdom.1252
pandas : 1.5.0.dev0+59.gad190575aa
numpy : 1.21.0
pytz : 2021.1
dateutil : 2.8.1
pip : 21.3.1
setuptools : 51.3.3
Cython : 0.29.24
pytest : 6.2.5
hypothesis : 6.35.0
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.1
IPython : 7.25.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.4.3
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.7.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None
zstandard : None
The text was updated successfully, but these errors were encountered: