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

Neil Schemenauer report at bugs.python.org
Wed Jan 24 19:13:20 EST 2018


Neil Schemenauer <nas-python at arctrix.com> added the comment:

Using atexit is not the solution because the data can be lost even while the program is running, not just at interpreter shutdown.  The problem occurs if the buffered file object and the underlying file object are both part of a reference cycle.  Then, when the cycle is destroyed by the GC module, if the file __del__ is called before the buffer __del__, data is lost.

So far, the best solution I've come up with is as follows:  split the buffered file object into two objects, a wrapper object that will be returned from open() and an underlying object that holds the actual buffer.  Make the underlying file object keep references to all the buffers that are using the file.  When the file _del__ gets called, first flush all of the buffers and then close the file.  Splitting the buffered file object is required so that we don't create reference cycles.

----------

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


More information about the Python-bugs-list mailing list