Skip to content

Commit f6ef8a6

Browse files
fix: propagate dispatch error to current task (#917)
Co-authored-by: Max Schmitt <[email protected]>
1 parent b7eb7d8 commit f6ef8a6

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

playwright/_impl/_connection.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,15 @@ async def inner_send(
4747
if params is None:
4848
params = {}
4949
callback = self._connection._send_message_to_server(self._guid, method, params)
50-
51-
done, pending = await asyncio.wait(
52-
{self._connection._transport.on_error_future, callback.future},
50+
if self._connection._error:
51+
error = self._connection._error
52+
self._connection._error = None
53+
raise error
54+
done, _ = await asyncio.wait(
55+
{
56+
self._connection._transport.on_error_future,
57+
callback.future,
58+
},
5359
return_when=asyncio.FIRST_COMPLETED,
5460
)
5561
if not callback.future.done():
@@ -152,10 +158,10 @@ def __init__(
152158
self._callbacks: Dict[int, ProtocolCallback] = {}
153159
self._object_factory = object_factory
154160
self._is_sync = False
155-
self._api_name = ""
156161
self._child_ws_connections: List["Connection"] = []
157162
self._loop = loop
158163
self._playwright_future: asyncio.Future["Playwright"] = loop.create_future()
164+
self._error: Optional[BaseException] = None
159165

160166
async def run_as_sync(self) -> None:
161167
self._is_sync = True
@@ -260,11 +266,10 @@ def _dispatch(self, msg: ParsedMessagePayload) -> None:
260266
g.switch(self._replace_guids_with_channels(params))
261267
else:
262268
object._channel.emit(method, self._replace_guids_with_channels(params))
263-
except Exception:
264-
print(
265-
"Error dispatching the event",
266-
"".join(traceback.format_exception(*sys.exc_info())),
267-
)
269+
except BaseException as exc:
270+
print("Error occured in event listener", file=sys.stderr)
271+
traceback.print_exc()
272+
self._error = exc
268273

269274
def _create_remote_object(
270275
self, parent: ChannelOwner, type: str, guid: str, initializer: Dict

tests/common/test_events.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import Dict
2+
3+
import pytest
4+
5+
from playwright.sync_api import sync_playwright
6+
7+
8+
def test_events(browser_name: str, launch_arguments: Dict) -> None:
9+
with pytest.raises(Exception, match="fail"):
10+
11+
def fail() -> None:
12+
raise Exception("fail")
13+
14+
with sync_playwright() as p:
15+
with p[browser_name].launch(**launch_arguments) as browser:
16+
with browser.new_page() as page:
17+
page.on("response", lambda _: fail())
18+
page.goto("https://example.com")

0 commit comments

Comments
 (0)