Skip to content

Commit 289259d

Browse files
authored
Add human readable error messages for WebSocket PING/PONG timeouts (#10422)
1 parent 51daf71 commit 289259d

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
@@ -186,7 +186,11 @@ def _ping_task_done(self, task: "asyncio.Task[None]") -> None:
186186

187187
def _pong_not_received(self) -> None:
188188
if self._req is not None and self._req.transport is not None:
189-
self._handle_ping_pong_exception(asyncio.TimeoutError())
189+
self._handle_ping_pong_exception(
190+
asyncio.TimeoutError(
191+
f"No PONG received after {self._pong_heartbeat} seconds"
192+
)
193+
)
190194

191195
def _handle_ping_pong_exception(self, exc: BaseException) -> None:
192196
"""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
@@ -926,6 +926,7 @@ async def handler(request: web.Request) -> NoReturn:
926926
assert resp.close_code is WSCloseCode.ABNORMAL_CLOSURE
927927
assert msg.type is WSMsgType.ERROR
928928
assert isinstance(msg.data, ServerTimeoutError)
929+
assert str(msg.data) == "No PONG received after 0.05 seconds"
929930

930931

931932
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
@@ -841,6 +841,7 @@ async def handler(request: web.Request) -> NoReturn:
841841
assert ws.close_code == WSCloseCode.ABNORMAL_CLOSURE
842842
assert ws_server_close_code == WSCloseCode.ABNORMAL_CLOSURE
843843
assert isinstance(ws_server_exception, asyncio.TimeoutError)
844+
assert str(ws_server_exception) == "No PONG received after 0.025 seconds"
844845
await ws.close()
845846

846847

0 commit comments

Comments
 (0)