diff --git a/CHANGES/8444.bugfix b/CHANGES/8444.bugfix new file mode 100644 index 00000000000..2fc1d5d829c --- /dev/null +++ b/CHANGES/8444.bugfix @@ -0,0 +1,2 @@ +Fix ``ws_connect`` not respecting ``timeout`` nor ``receive_timeout`` on WS(S) connection. +-- by :user:`arcivanov`. diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 870819b4b8c..3f4a257a678 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -47,6 +47,7 @@ Anes Abismail Antoine Pietri Anton Kasyanov Anton Zhdan-Pushkin +Arcadiy Ivanov Arie Bovenberg Arseny Timoniq Artem Yushkovskiy diff --git a/aiohttp/client.py b/aiohttp/client.py index 7a4db0db476..7f7a9492492 100644 --- a/aiohttp/client.py +++ b/aiohttp/client.py @@ -939,6 +939,13 @@ async def _ws_connect( assert conn is not None conn_proto = conn.protocol assert conn_proto is not None + # For WS connection the sock_read must be either receive_timeout + # or timeout (whichever is specified), unless read_timeout is greater + conn_proto.read_timeout = ( + max(receive_timeout or timeout, conn_proto.read_timeout) + if conn_proto.read_timeout + else (receive_timeout or timeout) + ) transport = conn.transport assert transport is not None reader: FlowControlDataQueue[WSMessage] = FlowControlDataQueue( diff --git a/aiohttp/client_proto.py b/aiohttp/client_proto.py index 7a247e1c591..ff76a7289b5 100644 --- a/aiohttp/client_proto.py +++ b/aiohttp/client_proto.py @@ -240,6 +240,14 @@ def _reschedule_timeout(self) -> None: def start_timeout(self) -> None: self._reschedule_timeout() + @property + def read_timeout(self) -> Optional[float]: + return self._read_timeout + + @read_timeout.setter + def read_timeout(self, read_timeout: Optional[float]) -> None: + self._read_timeout = read_timeout + def _on_read_timeout(self) -> None: exc = SocketTimeoutError("Timeout on reading data from socket") self.set_exception(exc)