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

Neil Schemenauer report at bugs.python.org
Tue Dec 19 13:42:23 EST 2017


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

Welp, another day another attempt.  As mentioned in the PR 4847, atexit is not the answer.  If the raw/buffered file pair are part of a reference cycle and the GC cleans it before atexit runs, then the buffered data can get lost.

I attempted to implement my weakref idea (i.e. raw file keeps a weakref to the buffered file, calls flush before the raw file gets closed).  That doesn't work either because the GC clears the weakref before calling __del__.

The attached patch "buffer_register_flush.txt" does seem to work.  The downside is that it creates a reference cycle between the raw and buffered file objects.  Perhaps that is not a problem since unless you call close() on the raw file, you will be leaking resources anyhow.  In the patch, calling close() removes the reference cycle.

I still feel like this is worth fixing, as ugly as the implementation is.

----------
assignee:  -> nascheme
Added file: https://bugs.python.org/file47340/buffer_register_flush.txt

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


More information about the Python-bugs-list mailing list