Skip to content

Commit

Permalink
[PR #10422/289259d1 backport][3.11] Add human readable error messages…
Browse files Browse the repository at this point in the history
… for WebSocket PING/PONG timeouts (#10431)

Co-authored-by: J. Nick Koston <[email protected]>
  • Loading branch information
patchback[bot] and bdraco authored Feb 6, 2025
1 parent c4523f3 commit 5942b0b
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGES/10422.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Added human-readable error messages to the exceptions for WebSocket disconnects due to PONG not being received -- by :user:`bdraco`.

Previously, the error messages were empty strings, which made it hard to determine what went wrong.
4 changes: 3 additions & 1 deletion aiohttp/client_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ def _ping_task_done(self, task: "asyncio.Task[None]") -> None:
self._ping_task = None

def _pong_not_received(self) -> None:
self._handle_ping_pong_exception(ServerTimeoutError())
self._handle_ping_pong_exception(
ServerTimeoutError(f"No PONG received after {self._pong_heartbeat} seconds")
)

def _handle_ping_pong_exception(self, exc: BaseException) -> None:
"""Handle exceptions raised during ping/pong processing."""
Expand Down
6 changes: 5 additions & 1 deletion aiohttp/web_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,11 @@ def _ping_task_done(self, task: "asyncio.Task[None]") -> None:

def _pong_not_received(self) -> None:
if self._req is not None and self._req.transport is not None:
self._handle_ping_pong_exception(asyncio.TimeoutError())
self._handle_ping_pong_exception(
asyncio.TimeoutError(
f"No PONG received after {self._pong_heartbeat} seconds"
)
)

def _handle_ping_pong_exception(self, exc: BaseException) -> None:
"""Handle exceptions raised during ping/pong processing."""
Expand Down
1 change: 1 addition & 0 deletions tests/test_client_ws_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,7 @@ async def handler(request):
assert resp.close_code is WSCloseCode.ABNORMAL_CLOSURE
assert msg.type is WSMsgType.ERROR
assert isinstance(msg.data, ServerTimeoutError)
assert str(msg.data) == "No PONG received after 0.05 seconds"


async def test_close_websocket_while_ping_inflight(
Expand Down
1 change: 1 addition & 0 deletions tests/test_web_websocket_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,7 @@ async def handler(request: web.Request) -> NoReturn:
assert ws.close_code == WSCloseCode.ABNORMAL_CLOSURE
assert ws_server_close_code == WSCloseCode.ABNORMAL_CLOSURE
assert isinstance(ws_server_exception, asyncio.TimeoutError)
assert str(ws_server_exception) == "No PONG received after 0.025 seconds"
await ws.close()


Expand Down

0 comments on commit 5942b0b

Please sign in to comment.