diff --git a/src/websockets/sync/connection.py b/src/websockets/sync/connection.py index e7807324..06ea00ef 100644 --- a/src/websockets/sync/connection.py +++ b/src/websockets/sync/connection.py @@ -923,8 +923,9 @@ def close_socket(self) -> None: # Calling protocol.receive_eof() is safe because it's idempotent. # This guarantees that the protocol state becomes CLOSED. - self.protocol.receive_eof() - assert self.protocol.state is CLOSED + with self.protocol_mutex: + self.protocol.receive_eof() + assert self.protocol.state is CLOSED # Abort recv() with a ConnectionClosed exception. self.recv_messages.close() diff --git a/tests/sync/test_client.py b/tests/sync/test_client.py index 7d817051..7ab8f4dd 100644 --- a/tests/sync/test_client.py +++ b/tests/sync/test_client.py @@ -151,7 +151,8 @@ def test_connection_closed_during_handshake(self): """Client reads EOF before receiving handshake response from server.""" def close_connection(self, request): - self.close_socket() + self.socket.shutdown(socket.SHUT_RDWR) + self.socket.close() with run_server(process_request=close_connection) as server: with self.assertRaises(InvalidMessage) as raised: