Skip to content
Closed
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions playwright/_impl/_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,8 @@ def dispatch(self, msg: ParsedMessagePayload) -> None:
# Event handlers like route/locatorHandlerTriggered require us to perform async work.
# In order to report their potential errors to the user, we need to catch it and store it in the connection
def _done_callback(future: asyncio.Future) -> None:
if future.cancelled():
return
exc = future.exception()
if exc:
self._on_event_listener_error(exc)
Expand Down
25 changes: 25 additions & 0 deletions tests/sync/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,3 +549,28 @@ def handler(message: ConsoleMessage) -> None:
def test_click_should_accept_timedelta_for_timeout(page: Page) -> None:
with pytest.raises(TimeoutError, match="Timeout 1ms exceeded"):
page.click("does-not-exist", timeout=timedelta(milliseconds=1))


def test_sync_event_listener_cancelled_future() -> None:
import asyncio
from unittest.mock import MagicMock
from playwright._impl._connection import Channel, Connection

loop = asyncio.new_event_loop()
conn = Connection(MagicMock(), MagicMock(), MagicMock(), loop=loop)
conn._is_sync = True

dummy_obj = MagicMock()
dummy_obj._guid = "dummy"
dummy_channel = Channel(conn, dummy_obj)
dummy_obj._channel = dummy_channel
conn._objects["dummy"] = dummy_obj

fut = loop.create_future()
fut.cancel()

dummy_channel._emitter.on("event", lambda p: fut)
conn.dispatch({"guid": "dummy", "method": "event", "params": {}})
loop.run_until_complete(asyncio.sleep(0.01))
loop.close()