Author twisteroid ambassador
Recipients twisteroid ambassador, yselivanov
Date 2017-09-30.04:42:42
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1506746563.39.0.213398074469.issue31647@psf.upfronthosting.co.za>
In-reply-to
Content
Currently, if one attempts to do write_eof() on a StreamWriter after the underlying transport is already closed, an AttributeError is raised:


Traceback (most recent call last):
  File "<snip>\scratch_3.py", line 34, in main_coro
    writer.write_eof()
  File "C:\Program Files\Python36\lib\asyncio\streams.py", line 300, in write_eof
    return self._transport.write_eof()
  File "C:\Program Files\Python36\lib\asyncio\selector_events.py", line 808, in write_eof
    self._sock.shutdown(socket.SHUT_WR)
AttributeError: 'NoneType' object has no attribute 'shutdown'


This is because _SelectorSocketTransport.write_eof() only checks for self._eof before calling self._sock.shutdown(), and self._sock has already been assigned None after _call_connection_lost().

Compare with StreamWriter.write() after close, which either does nothing or logs a warning after 5 attempts; or StreamWriter.drain() after close, which raises a ConnectionResetError; or even StreamWriter.close() after close, which does nothing.

Trying to do write_eof() after close may happen unintentionally, for example when the following sequence of events happen:
* the remote side closes the connection
* the local side attempts to write, so the socket "figures out" the connection is closed, and close this side of the socket. Note the write fails silently, except when loop.set_debug(True) where asyncio logs "Fatal write error on socket transport".
* the local side does write_eof(). An AttributError is raised.

Currently the only way to handle this gracefully is to either catch AttributeError or check StreamWriter.transport.is_closing() before write_eof(). Neither is pretty.

I suggest making write_eof() after close either do nothing, or raise a subclass of ConnectionError. Both will be easier to handle then the current behavior.

Attached repro.
History
Date User Action Args
2017-09-30 04:42:43twisteroid ambassadorsetrecipients: + twisteroid ambassador, yselivanov
2017-09-30 04:42:43twisteroid ambassadorsetmessageid: <1506746563.39.0.213398074469.issue31647@psf.upfronthosting.co.za>
2017-09-30 04:42:43twisteroid ambassadorlinkissue31647 messages
2017-09-30 04:42:42twisteroid ambassadorcreate