Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consume ConnectionResetError in ping tasks #7238

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion aiohttp/client_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def _send_heartbeat(self) -> None:
# fire-and-forget a task is not perfect but maybe ok for
# sending ping. Otherwise we need a long-living heartbeat
# task in the class.
self._loop.create_task(self._writer.ping())
self._loop.create_task(self._writer.ping(consume_errors=True))

if self._pong_response_cb is not None:
self._pong_response_cb.cancel()
Expand Down
8 changes: 6 additions & 2 deletions aiohttp/http_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,11 +667,15 @@ async def pong(self, message: bytes = b"") -> None:
message = message.encode("utf-8")
await self._send_frame(message, WSMsgType.PONG)

async def ping(self, message: bytes = b"") -> None:
async def ping(self, message: bytes = b"", consume_errors: bool = False) -> None:
altendky marked this conversation as resolved.
Show resolved Hide resolved
"""Send ping message."""
if isinstance(message, str):
message = message.encode("utf-8")
await self._send_frame(message, WSMsgType.PING)
try:
await self._send_frame(message, WSMsgType.PING)
except ConnectionResetError:
if not consume_errors:
raise

async def send(
self,
Expand Down
2 changes: 1 addition & 1 deletion aiohttp/web_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def _send_heartbeat(self) -> None:
# fire-and-forget a task is not perfect but maybe ok for
# sending ping. Otherwise we need a long-living heartbeat
# task in the class.
self._loop.create_task(self._writer.ping()) # type: ignore[union-attr]
self._loop.create_task(self._writer.ping(consume_errors=True)) # type: ignore[union-attr]

if self._pong_response_cb is not None:
self._pong_response_cb.cancel()
Expand Down