[issue17852] Built-in module _io can loose data from buffered files at exit

Neil Schemenauer report at bugs.python.org
Wed May 31 16:08:39 EDT 2017


Neil Schemenauer added the comment:

Well, I just spent a couple of hours debugging a problem caused by this issue.  You could argue that I should be calling close() on all of my file-like objects but I agree with Armin that the current "most of the time it works" behaviour is quite poor.

In my case, the issue is exactly what Antoine Pitrou suggests: if the FileIO object gets finalized before the BufferedIO object wrapping it then the buffered data gets lost. That depends on the order that the GC calls finalizers.

Armin's suggestion of keeping a list of open buffered files and flushing them before exiting seems like a simple and robust solution.  Having a warning for unclosed files is fine but in order to make porting Python 2 code as painless as possible, matching the safer behavior of Python 2 would be worth the extra bit of flush logic.

----------
nosy: +nascheme

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


More information about the Python-bugs-list mailing list