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

chore: create WebSocket reply only calls using own loop #2626

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading