Skip to content

Commit 5942b0b

Browse files
[PR #10422/289259d1 backport][3.11] Add human readable error messages for WebSocket PING/PONG timeouts (#10431)
Co-authored-by: J. Nick Koston <[email protected]>
1 parent c4523f3 commit 5942b0b

File tree

5 files changed

+13
-2
lines changed

5 files changed

+13
-2
lines changed

CHANGES/10422.misc.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Added human-readable error messages to the exceptions for WebSocket disconnects due to PONG not being received -- by :user:`bdraco`.
2+
3+
Previously, the error messages were empty strings, which made it hard to determine what went wrong.

aiohttp/client_ws.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ def _ping_task_done(self, task: "asyncio.Task[None]") -> None:
163163
self._ping_task = None
164164

165165
def _pong_not_received(self) -> None:
166-
self._handle_ping_pong_exception(ServerTimeoutError())
166+
self._handle_ping_pong_exception(
167+
ServerTimeoutError(f"No PONG received after {self._pong_heartbeat} seconds")
168+
)
167169

168170
def _handle_ping_pong_exception(self, exc: BaseException) -> None:
169171
"""Handle exceptions raised during ping/pong processing."""

aiohttp/web_ws.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,11 @@ def _ping_task_done(self, task: "asyncio.Task[None]") -> None:
182182

183183
def _pong_not_received(self) -> None:
184184
if self._req is not None and self._req.transport is not None:
185-
self._handle_ping_pong_exception(asyncio.TimeoutError())
185+
self._handle_ping_pong_exception(
186+
asyncio.TimeoutError(
187+
f"No PONG received after {self._pong_heartbeat} seconds"
188+
)
189+
)
186190

187191
def _handle_ping_pong_exception(self, exc: BaseException) -> None:
188192
"""Handle exceptions raised during ping/pong processing."""

tests/test_client_ws_functional.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,7 @@ async def handler(request):
902902
assert resp.close_code is WSCloseCode.ABNORMAL_CLOSURE
903903
assert msg.type is WSMsgType.ERROR
904904
assert isinstance(msg.data, ServerTimeoutError)
905+
assert str(msg.data) == "No PONG received after 0.05 seconds"
905906

906907

907908
async def test_close_websocket_while_ping_inflight(

tests/test_web_websocket_functional.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,7 @@ async def handler(request: web.Request) -> NoReturn:
797797
assert ws.close_code == WSCloseCode.ABNORMAL_CLOSURE
798798
assert ws_server_close_code == WSCloseCode.ABNORMAL_CLOSURE
799799
assert isinstance(ws_server_exception, asyncio.TimeoutError)
800+
assert str(ws_server_exception) == "No PONG received after 0.025 seconds"
800801
await ws.close()
801802

802803

0 commit comments

Comments
 (0)