[issue25994] File descriptor leaks in os.scandir()

STINNER Victor report at bugs.python.org
Sat Jan 2 17:26:20 EST 2016


STINNER Victor added the comment:

Hi,

If I recall correctly, this issue was discussed in the long review of os.scandir(): issue #22524.


"os.scandir() opens a file descriptor and closes it only in its destructor."

Hopefully, it's incorrect: it's closed when the iterator is exhausted. See how ScandirIterator_close() is used.


"This can causes file descriptor leaks in Python implementations without reference counting"

Destructors are part of the Python language. Are you aware of a Python implementation *never* calls destructors? I know that PyPy can call destructors "later", not necessary when the last reference to the object is removed. Do you think that we may reach the file descriptor limit because of that?


"We need to add the close() method to the scandir iterator (as in files and generators)."

Is it part of the iterator protocol? Or do you mean to explicitly call close()?


"It would be useful also to make it a context manager."

If we decide to add a close() method, it like the idea of also supporting the context manager protocol.


"In 3.5 we have to add a warning about this behavior."

Yeah, maybe we can elaborate the doc to explain how the file descriptor / Windows handle is used.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue25994>
_______________________________________


More information about the Python-bugs-list mailing list