Repository navigation
Reading received data from a closed TCP stream using StreamReader.read might hang forever #79246
Description
Activity
I'm not sure whether it is intended or not, but I noticed a change in the behavior of
StreamReaderbetween version 3.7 and 3.8.Basically, reading some received data from a closed TCP stream using
StreamReader.readmight hang forever, under certain conditions.I'm not sure what those conditions are but I managed to reproduce the issue consistently with the following workflow:
- server writes some data
- client reads a part of the data
- client closes the writer
- server closes the writer
- client tries to read the remaining data
The test attached implements the behavior. It fails on 3.8 but passes on 3.7
Hi Vincent!
No, the hang is not intended behavior. Thanks for the report.
I'll assign the issue to myself to don't miss the bug; but if you have a patch with a fix -- please don't hesitate to make a Pull Request for it.Hi Andrew!
I reverted the commit associated with the following PR, and the hanging issue disappeared:
#9201I'll look into it.
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Oct 26, 2018 I found the culprit:
cpython/Lib/asyncio/streams.py
Line 350 in a05bef4
self._protocol._untrack_reader() The call to
_untrack_readeris performed too soon. Closing the transport causesprotocol.connection_lost()to be "called soon" but by the time it is actually executed, the stream reader has been "untracked". Since the protocol doesn't know the stream reader anymore, it has not way to feed it the EOF.The fix attached removes the
_untrack_readercall and definition altogether. I don't really see the point of this method since one has to wait forconnection_lostto be executed before untracking the reader, butconnection_lostalready gets rid of the reader reference.With this fix, calling
writer.closethen awaitingwriter.wait_closed(or awaitingwriter.aclose) should:- close the transport
- schedule
protocol.connection_lost - wait for the protocol to be closed
- run
protocol.connection_lost - feed the EOF to the reader
- set the protocol as closed
- get rid of the reader reference in the protocol
- return (making aclose causal and safe)
- the reader can then be safely garbage collected
But maybe I'm missing something about
_untrack_reader?
StreamReaderProtocol._untrack_reader#10212Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: