[New-bugs-announce] [issue42830] tempfile mkstemp() leaks file descriptors if os.close() is not called

Mieczysław Torchała report at bugs.python.org
Tue Jan 5 06:28:47 EST 2021


New submission from Mieczysław Torchała <mieczyslaw.torchala at gmail.com>:

tempfile mkstemp() documentation says: "Unlike TemporaryFile(), the user of mkstemp() is responsible for deleting the temporary file when done with it."

mkstemp() returns a tuple:

file_descriptor, file_path = mkstemp()

Calling only 

os.unlink(file_path) 

removes the file, but causes leaking file descriptors and when the number of temporary files created is higher than `ulimit -n`, the process crashes (see /proc/$pid/fd in real time until crash).

The solution I found is to also call on descriptor:

os.close(file_descriptor)

but the documentation doesn't mention that (i.e. releasing file descriptor in addition to removing temporary file).

For many users it doesn't matter as they create a few files and when the process finishes, leaking file descriptors are released. 

However, when a lot of files is created during the execution, it will finally crash (unless someone has a huge ulimit -n setting).

If this is not a bug, at least the documentation should mention that both the temp file needs to be removed and the file descriptor released. However, this means calling two commands when only one command was used to create the temporary file. Therefore, maybe adding a function to tempfile library to fully remove a file without a leaking file descriptor is a solution.

----------
components: Library (Lib)
messages: 384384
nosy: mieczyslaw.torchala
priority: normal
severity: normal
status: open
title: tempfile mkstemp() leaks file descriptors if os.close() is not called
type: resource usage
versions: Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42830>
_______________________________________


More information about the New-bugs-announce mailing list