◐ Shell
clean mode source ↗

bpo-17852: Fix PR #3372, flush BufferedWriter objects on exit. by nascheme · Pull Request #4847 · python/cpython

Closing this PR as it is not really a fix. It works if the reference cycle containing the raw file and buffered file object are not re-claimed before atexit gets run. However, the cycle can be claimed during a normal gc.collect() run. In that case, the raw file can get closed before the buffered file and data in the buffer will be discarded.

I thought having the raw file keep a list of weak refs to the buffer and calling flush() on those when the raw close() is called would be a proper fix. However, that doesn't work either. The GC clears the weakrefs in handle_weakrefs() before it calls __del__ on the raw file object. So, the raw file cannot get the buffer to flush itself before the raw file is closed. So, using weak refs does not work, at least with how gcmodule works at the moment.

The only other idea I have is to split the buffered IO object into two parts. A "proxy" object that wraps the underlying state. The raw file object would keep a strong reference to this underlying state object.Then the raw file can call flush() before closing itself.