[pypy-issue] Issue #3021: `io.open(directory)` leaks a file descriptor (pypy/pypy)

Anthony at bitbucket.org Anthony at bitbucket.org
Tue Jun 4 14:42:31 EDT 2019


New issue 3021: `io.open(directory)` leaks a file descriptor
https://bitbucket.org/pypy/pypy/issues/3021/ioopen-directory-leaks-a-file-descriptor

Anthony Sottile:

This is the minimal setup that I’ve been able to reproduce this with:

‌

```python
import io
import pytest
import warnings

def test():
    for _ in range(15000):
        with pytest.raises(IsADirectoryError):
            with io.open('.'):
                pass


with warnings.catch_warnings(record=True) as wrns:
    warnings.simplefilter('always')
    test()

if len(wrns):
    print('*' * 79)
    print('warnings: {}'.format(len(wrns)))
    print('first warning:')
    print('{}:{}:{}'.format(wrns[0].filename, wrns[0].lineno, wrns[0].message))
    print('*' * 79)
```

‌

On my machine the fd limits are pretty low so I reproduced this easier in docker or as root with `ulimit -n ...`

```shell
(venvpp) root at asottile-MacBookPro:/tmp# python  t.py
*******************************************************************************
warnings: 10422
first warning:
t.py:8:unclosed file <_io.FileIO fd=3 mode='rb' closefd=True>
*******************************************************************************
```

‌

I’ve tried the following and both have reproduced:

```
# python --version
Python 3.6.1 (784b254d6699, Apr 14 2019, 10:22:42)
[PyPy 7.1.1-beta0 with GCC 6.2.0 20160901]
```

```
# /venv/bin/python --version
Python 3.5.3 (928a4f70d3de, Feb 08 2019, 10:42:58)
[PyPy 7.0.0 with GCC 6.2.0 20160901]
```

These are the packages I’ve been using, though it doesn’t appear to make much difference:

```
# pip freeze -l
atomicwrites==1.3.0
attrs==19.1.0
importlib-metadata==0.17
more-itertools==7.0.0
packaging==19.0
pluggy==0.12.0
py==1.8.0
pyparsing==2.4.0
pytest==4.6.2
six==1.12.0
wcwidth==0.1.7
zipp==0.5.1
```

Original investigation was done here: [https://github.com/nedbat/coveragepy/issues/807](https://github.com/nedbat/coveragepy/issues/807)

Related: [https://github.com/pytest-dev/pytest/issues/5342](https://github.com/pytest-dev/pytest/issues/5342)

I cannot reproduce this with pypy2.x:

```
$ pypy --version
Python 2.7.13 (8cdda8b8cdb8, Apr 14 2019, 14:06:44)
[PyPy 7.1.1 with GCC 6.2.0 20160901]
```




More information about the pypy-issue mailing list