New submission from Mieczysław Torchała <mieczyslaw.torch...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue42830>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to