From a7ecf966193ac8af14ba50fad1c7320f6a349b80 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sat, 6 Nov 2021 09:01:11 +0100 Subject: [PATCH] Avoid shutdown on closed socket. Fix #1072. --- src/websockets/legacy/protocol.py | 2 +- tests/legacy/test_protocol.py | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/websockets/legacy/protocol.py b/src/websockets/legacy/protocol.py index 3340c33be..23d46d018 100644 --- a/src/websockets/legacy/protocol.py +++ b/src/websockets/legacy/protocol.py @@ -1303,7 +1303,7 @@ async def close_connection(self) -> None: self.logger.debug("! timed out waiting for TCP close") # Half-close the TCP connection if possible (when there's no TLS). - if self.transport.can_write_eof(): + if self.transport.can_write_eof() and not self.transport.is_closing(): if self.debug: self.logger.debug("x half-closing TCP connection") self.transport.write_eof() diff --git a/tests/legacy/test_protocol.py b/tests/legacy/test_protocol.py index 1672ab1ed..22e72a696 100644 --- a/tests/legacy/test_protocol.py +++ b/tests/legacy/test_protocol.py @@ -59,6 +59,9 @@ def setup_mock(self, loop, protocol): def can_write_eof(self): return True + def is_closing(self): + return False + def write_eof(self): # When the protocol half-closes the TCP connection, it expects the # other end to close it. Simulate that.