Skip to content

Commit 555a2ee

Browse files
committed
iostream: Treat connection resets as a clean close in read_until_close
The change to report errors to a pending read_until_close broke the SSL variants of test_read_until_close_after_close on windows. There, closing one side of the connection makes the other side's next read fail with ECONNRESET instead of returning a clean EOF, so self.error was set and the pending read failed with StreamClosedError instead of returning the data that had already been buffered. IOStream treats a connection reset as a normal close everywhere else (see _read_to_buffer, which closes the stream and returns instead of raising), precisely because a clean shutdown by the peer can be reported this way. Do the same here: resolve a pending read_until_close from the buffer when the stream is closed cleanly or by a reset, and leave it pending (to be failed by _signal_closed) only for other errors such as StreamBufferFullError. Add a regression test that simulates the windows behavior by turning the EOF from read_from_fd into an ECONNRESET, so this is covered on all platforms. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011PBzMWughm8fUFZDTrd8QF
1 parent 3ba622f commit 555a2ee

3 files changed

Lines changed: 44 additions & 9 deletions

File tree

‎docs/releases/v6.5.9.rst‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Bug fixes
3838
`tornado.iostream`
3939
~~~~~~~~~~~~~~~~~~
4040

41-
- ``.IOStream.read_until_close`` now reports errors correctly when a stream closes uncleanly
42-
or exceeds a memory limit. Previously it would report a successful read of whatever was in the
43-
buffer.
41+
- ``.IOStream.read_until_close`` now reports errors correctly when a stream is closed due to
42+
an error or exceeds a memory limit. Previously it would report a successful read of whatever
43+
was in the buffer. Connection resets are still treated as a normal close (as they are
44+
elsewhere in `.IOStream`), since some platforms report a clean shutdown by the peer this way.

‎tornado/iostream.py‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -588,13 +588,18 @@ def close(
588588
self.error = exc_info[1]
589589
if self._read_until_close:
590590
self._read_until_close = False
591-
if self.error is None:
591+
if self.error is None or self._is_connreset(self.error):
592+
# A connection reset is treated as a normal close
593+
# throughout this class (on some platforms, notably
594+
# windows, a peer that closes cleanly may still be
595+
# reported as a reset), so deliver the buffered data as
596+
# the result of the read.
592597
self._finish_read(self._read_buffer_size)
593-
# If the stream is closing because of an error, leave the
594-
# read future pending so that _signal_closed() fails it with
595-
# StreamClosedError(real_error=self.error). Resolving it with
596-
# the buffered data would report a truncated result as if it
597-
# were complete.
598+
# Otherwise the stream is closing because of a real error, so
599+
# leave the read future pending for _signal_closed() to fail
600+
# with StreamClosedError(real_error=self.error). Resolving it
601+
# with the buffered data would report a truncated result as if
602+
# it were complete.
598603
elif self._read_future is not None:
599604
# resolve reads that are pending and ready to complete
600605
try:

‎tornado/test/iostream_test.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,35 @@ def test_read_until_close_after_error_close(self):
847847
server.close()
848848
client.close()
849849

850+
@gen_test
851+
def test_read_until_close_after_connection_reset(self):
852+
# A connection reset is treated as a normal close throughout
853+
# IOStream: on some platforms (notably windows) a peer that closes
854+
# cleanly may be reported to us as a reset, so a pending
855+
# read_until_close must still return the buffered data instead of
856+
# failing.
857+
rs, ws = yield self.make_iostream_pair()
858+
try:
859+
real_read_from_fd = rs.read_from_fd
860+
861+
def read_from_fd(buf):
862+
result = real_read_from_fd(buf)
863+
if result == 0:
864+
# Report the EOF as a reset instead.
865+
raise ConnectionResetError(errno.ECONNRESET, "Connection reset")
866+
return result
867+
868+
rs.read_from_fd = read_from_fd # type: ignore[method-assign]
869+
ws.write(b"1234")
870+
data = yield rs.read_bytes(1)
871+
self.assertEqual(data, b"1")
872+
ws.close()
873+
data = yield rs.read_until_close()
874+
self.assertEqual(data, b"234")
875+
finally:
876+
ws.close()
877+
rs.close()
878+
850879
@gen_test
851880
def test_read_until_close_with_buffer_overflow(self):
852881
# Overflowing the read buffer during a read_until_close must raise

0 commit comments

Comments
 (0)