New submission from Robert Smallshire:

Note: This report is based on a close reading of the asyncio code, rather than 
having experienced defective behaviour in test or production.

The documentation for BaseTransport.close() states: "Close the transport. If 
the transport has a buffer for outgoing data, buffered data will be flushed 
asynchronously."

The _UnixWritePipeTransport._close method, which is called by close() contains 
this code in Python 3.5.1

    def _close(self, exc=None):
        self._closing = True
        if self._buffer:
            self._loop.remove_writer(self._fileno)
        self._buffer.clear()
        self._loop.remove_reader(self._fileno)
        self._loop.call_soon(self._call_connection_lost, exc)

In this context, _buffer is a list of bytes objects comprising 
yet-to-be-written data. Note that close() removes the writer if _buffer is 
*not* empty, so the buffered data will never be written.  I believe this 
conditional should be inverted, so the writer is removed only if the buffer is 
*empty*. So:

        if not self._buffer:
            self._loop.remove_writer(self._fileno)

Arguably though, a more robust and easier to read test would be to call 
get_write_buffer_size(), like this:

        if self.get_write_buffer_size() == 0:
            self._loop.remove_writer(self._fileno)

Note that in the event that the writer is not removed by _close(), the writer 
will
remove itself when the buffer does eventually become empty.

Assuming my analysis is correct, and a fix is made to close(), then abort() 
will also need to be modified to *not* flush buffered writes, as the 
documentation for abort() states "Close the transport immediately, without 
waiting for pending operations to complete. Buffered data will be lost."

----------
components: asyncio
messages: 262601
nosy: Robert Smallshire, gvanrossum, haypo, yselivanov
priority: normal
severity: normal
status: open
title: asyncio _UnixWritePipeTransport._close abandons unflushed writes
versions: Python 3.5

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue26663>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to