Skip to content

Commit 63ff66e

Browse files
fix: ignore CancelledError in sync event listener done callback
1 parent 3e7abc4 commit 63ff66e

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

‎playwright/_impl/_connection.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,8 @@ def dispatch(self, msg: ParsedMessagePayload) -> None:
435435
# Event handlers like route/locatorHandlerTriggered require us to perform async work.
436436
# In order to report their potential errors to the user, we need to catch it and store it in the connection
437437
def _done_callback(future: asyncio.Future) -> None:
438+
if future.cancelled():
439+
return
438440
exc = future.exception()
439441
if exc:
440442
self._on_event_listener_error(exc)

‎tests/sync/test_sync.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,3 +549,28 @@ def handler(message: ConsoleMessage) -> None:
549549
def test_click_should_accept_timedelta_for_timeout(page: Page) -> None:
550550
with pytest.raises(TimeoutError, match="Timeout 1ms exceeded"):
551551
page.click("does-not-exist", timeout=timedelta(milliseconds=1))
552+
553+
554+
def test_sync_event_listener_cancelled_future() -> None:
555+
import asyncio
556+
from unittest.mock import MagicMock
557+
from playwright._impl._connection import Channel, Connection
558+
559+
loop = asyncio.new_event_loop()
560+
conn = Connection(MagicMock(), MagicMock(), MagicMock(), loop=loop)
561+
conn._is_sync = True
562+
563+
dummy_obj = MagicMock()
564+
dummy_obj._guid = "dummy"
565+
dummy_channel = Channel(conn, dummy_obj)
566+
dummy_obj._channel = dummy_channel
567+
conn._objects["dummy"] = dummy_obj
568+
569+
fut = loop.create_future()
570+
fut.cancel()
571+
572+
dummy_channel._emitter.on("event", lambda p: fut)
573+
conn.dispatch({"guid": "dummy", "method": "event", "params": {}})
574+
loop.run_until_complete(asyncio.sleep(0.01))
575+
loop.close()
576+

0 commit comments

Comments
 (0)