Skip to content

Commit 5c9ad62

Browse files
committed
Maintain a list of BufferedWriter objects. Flush them on exit.
In Python 3, the buffer and the underlying file object are separate and so the order in which objects are finalized matters. This is unlike Python 2 where the file and buffer were a single object and finalization was done for both at the same time. In Python 3, if the file is finalized and closed before the buffer then the data in the buffer is lost. This change adds a doubly linked list of open file buffers. An atexit hook ensures they are flushed before proceeding with interpreter shutdown. This is addition does not remove the need to properly close files as there are other reasons why buffered data could get lost during finalization. Initial patch by Armin Rigo.
1 parent af64aff commit 5c9ad62

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

Lib/_pyio.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,7 @@ def __init__(self, raw, buffer_size=DEFAULT_BUFFER_SIZE):
11851185
self.buffer_size = buffer_size
11861186
self._write_buf = bytearray()
11871187
self._write_lock = Lock()
1188+
_register_writer(self)
11881189

11891190
def writable(self):
11901191
return self.raw.writable()
@@ -2574,3 +2575,23 @@ def encoding(self):
25742575
def detach(self):
25752576
# This doesn't make sense on StringIO.
25762577
self._unsupported("detach")
2578+
2579+
2580+
# ____________________________________________________________
2581+
2582+
import atexit, weakref
2583+
2584+
_all_writers = weakref.WeakKeyDictionary()
2585+
2586+
def _register_writer(w):
2587+
# keep weak-ref to buffered writer
2588+
_all_writers[w] = True
2589+
2590+
def _flush_all_writers():
2591+
# Ensure all buffered writers are flushed before proceeding with
2592+
# normal shutdown. Otherwise, if the underlying file objects get
2593+
# finalized before the buffered writer wrapping it then any buffered
2594+
# data will be lost.
2595+
for w in _all_writers:
2596+
w.flush()
2597+
atexit.register(_flush_all_writers)

Modules/_io/_iomodule.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,8 @@ PyInit__io(void)
766766
!(_PyIO_empty_bytes = PyBytes_FromStringAndSize(NULL, 0)))
767767
goto fail;
768768

769+
_Py_PyAtExit(_PyIO_atexit_flush);
770+
769771
state->initialized = 1;
770772

771773
return m;

Modules/_io/_iomodule.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,5 @@ extern PyObject *_PyIO_empty_str;
183183
extern PyObject *_PyIO_empty_bytes;
184184

185185
extern PyTypeObject _PyBytesIOBuffer_Type;
186+
187+
extern void _PyIO_atexit_flush(void);

Modules/_io/bufferedio.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ bufferediobase_write(PyObject *self, PyObject *args)
196196
}
197197

198198

199+
struct doubly_linked_s {
200+
struct doubly_linked_s *prev, *next;
201+
};
202+
199203
typedef struct {
200204
PyObject_HEAD
201205

@@ -240,8 +244,15 @@ typedef struct {
240244

241245
PyObject *dict;
242246
PyObject *weakreflist;
247+
248+
/* a doubly-linked chained list of "buffered" objects that need to
249+
be flushed when the process exits */
250+
struct doubly_linked_s buffered_writers_list;
243251
} buffered;
244252

253+
static struct doubly_linked_s doubly_linked_end = {
254+
&doubly_linked_end, &doubly_linked_end };
255+
245256
/*
246257
Implementation notes:
247258
@@ -386,6 +397,15 @@ _enter_buffered_busy(buffered *self)
386397
(self->buffer_size * (size / self->buffer_size)))
387398

388399

400+
static void
401+
remove_from_linked_list(buffered *self)
402+
{
403+
self->buffered_writers_list.next->prev = self->buffered_writers_list.prev;
404+
self->buffered_writers_list.prev->next = self->buffered_writers_list.next;
405+
self->buffered_writers_list.prev = NULL;
406+
self->buffered_writers_list.next = NULL;
407+
}
408+
389409
static void
390410
buffered_dealloc(buffered *self)
391411
{
@@ -394,6 +414,8 @@ buffered_dealloc(buffered *self)
394414
return;
395415
_PyObject_GC_UNTRACK(self);
396416
self->ok = 0;
417+
if (self->buffered_writers_list.next != NULL)
418+
remove_from_linked_list(self);
397419
if (self->weakreflist != NULL)
398420
PyObject_ClearWeakRefs((PyObject *)self);
399421
Py_CLEAR(self->raw);
@@ -1817,10 +1839,34 @@ _io_BufferedWriter___init___impl(buffered *self, PyObject *raw,
18171839
self->fast_closed_checks = (Py_TYPE(self) == &PyBufferedWriter_Type &&
18181840
Py_TYPE(raw) == &PyFileIO_Type);
18191841

1842+
if (self->buffered_writers_list.next == NULL) {
1843+
self->buffered_writers_list.prev = &doubly_linked_end;
1844+
self->buffered_writers_list.next = doubly_linked_end.next;
1845+
doubly_linked_end.next->prev = &self->buffered_writers_list;
1846+
doubly_linked_end.next = &self->buffered_writers_list;
1847+
}
1848+
18201849
self->ok = 1;
18211850
return 0;
18221851
}
18231852

1853+
/*
1854+
* Ensure all buffered writers are flushed before proceeding with
1855+
* normal shutdown. Otherwise, if the underlying file objects get
1856+
* finalized before the buffered writer wrapping it then any buffered
1857+
* data will be lost.
1858+
*/
1859+
void _PyIO_atexit_flush(void)
1860+
{
1861+
while (doubly_linked_end.next != &doubly_linked_end) {
1862+
buffered *buf = (buffered *)(((char *)doubly_linked_end.next) -
1863+
offsetof(buffered, buffered_writers_list));
1864+
remove_from_linked_list(buf);
1865+
buffered_flush(buf, NULL);
1866+
PyErr_Clear();
1867+
}
1868+
}
1869+
18241870
static Py_ssize_t
18251871
_bufferedwriter_raw_write(buffered *self, char *start, Py_ssize_t len)
18261872
{

0 commit comments

Comments
 (0)