Skip to content

Commit

Permalink
chore: create WebSocket reply only calls using own loop (#2626)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt authored Oct 31, 2024
1 parent 9d6adda commit 286d49e
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions playwright/_impl/_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,14 +541,16 @@ async def _race_with_page_close(self, future: Coroutine) -> None:
await asyncio.gather(fut, return_exceptions=True)


def _create_task_and_ignore_exception(coro: Coroutine) -> None:
def _create_task_and_ignore_exception(
loop: asyncio.AbstractEventLoop, coro: Coroutine
) -> None:
async def _ignore_exception() -> None:
try:
await coro
except Exception:
pass

asyncio.create_task(_ignore_exception())
loop.create_task(_ignore_exception())


class ServerWebSocketRoute:
Expand All @@ -572,29 +574,32 @@ def url(self) -> str:

def close(self, code: int = None, reason: str = None) -> None:
_create_task_and_ignore_exception(
self._ws._loop,
self._ws._channel.send(
"closeServer",
{
"code": code,
"reason": reason,
"wasClean": True,
},
)
),
)

def send(self, message: Union[str, bytes]) -> None:
if isinstance(message, str):
_create_task_and_ignore_exception(
self._ws._loop,
self._ws._channel.send(
"sendToServer", {"message": message, "isBase64": False}
)
),
)
else:
_create_task_and_ignore_exception(
self._ws._loop,
self._ws._channel.send(
"sendToServer",
{"message": base64.b64encode(message).decode(), "isBase64": True},
)
),
)


Expand Down Expand Up @@ -628,7 +633,9 @@ def _channel_message_from_page(self, event: Dict) -> None:
else event["message"]
)
elif self._connected:
_create_task_and_ignore_exception(self._channel.send("sendToServer", event))
_create_task_and_ignore_exception(
self._loop, self._channel.send("sendToServer", event)
)

def _channel_message_from_server(self, event: Dict) -> None:
if self._on_server_message:
Expand All @@ -638,19 +645,25 @@ def _channel_message_from_server(self, event: Dict) -> None:
else event["message"]
)
else:
_create_task_and_ignore_exception(self._channel.send("sendToPage", event))
_create_task_and_ignore_exception(
self._loop, self._channel.send("sendToPage", event)
)

def _channel_close_page(self, event: Dict) -> None:
if self._on_page_close:
self._on_page_close(event["code"], event["reason"])
else:
_create_task_and_ignore_exception(self._channel.send("closeServer", event))
_create_task_and_ignore_exception(
self._loop, self._channel.send("closeServer", event)
)

def _channel_close_server(self, event: Dict) -> None:
if self._on_server_close:
self._on_server_close(event["code"], event["reason"])
else:
_create_task_and_ignore_exception(self._channel.send("closePage", event))
_create_task_and_ignore_exception(
self._loop, self._channel.send("closePage", event)
)

@property
def url(self) -> str:
Expand All @@ -674,19 +687,21 @@ def connect_to_server(self) -> "WebSocketRoute":
def send(self, message: Union[str, bytes]) -> None:
if isinstance(message, str):
_create_task_and_ignore_exception(
self._loop,
self._channel.send(
"sendToPage", {"message": message, "isBase64": False}
)
),
)
else:
_create_task_and_ignore_exception(
self._loop,
self._channel.send(
"sendToPage",
{
"message": base64.b64encode(message).decode(),
"isBase64": True,
},
)
),
)

def on_message(self, handler: Callable[[Union[str, bytes]], Any]) -> None:
Expand Down

0 comments on commit 286d49e

Please sign in to comment.