diff --git a/playwright/_impl/waiter.py b/playwright/_impl/waiter.py index 0878dfe79..f4f3fb78f 100644 --- a/playwright/_impl/waiter.py +++ b/playwright/_impl/waiter.py @@ -240,7 +240,7 @@ def settle(matched: bool) -> None: if not promise.done(): promise.set_result(event_arg) - def fail(e: Exception) -> None: + def fail(e: BaseException) -> None: emitter.remove_listener(event, listener) if not promise.done(): promise.set_exception(e) @@ -256,6 +256,9 @@ def run() -> Any: async def continuation() -> None: try: settle(bool(await matched)) + except asyncio.CancelledError as e: + fail(e) + raise except Exception as e: fail(e) diff --git a/tests/async/python/test_page.py b/tests/async/python/test_page.py index a942df552..4569cd878 100644 --- a/tests/async/python/test_page.py +++ b/tests/async/python/test_page.py @@ -14,6 +14,7 @@ """Python only: tests of behaviors that no upstream test covers.""" +import asyncio from typing import Any, AsyncGenerator, Dict import pytest @@ -59,6 +60,19 @@ async def predicate(response: Any) -> bool: await page.evaluate("() => fetch('/digits/1.png')") +async def test_expect_response_should_propagate_async_predicate_cancellation( + page: Page, server: Server +) -> None: + await page.goto(server.EMPTY_PAGE) + + async def predicate(response: Any) -> bool: + raise asyncio.CancelledError("Predicate cancelled") + + with pytest.raises(asyncio.CancelledError, match="Predicate cancelled"): + async with page.expect_response(predicate): + await page.evaluate("() => fetch('/digits/1.png')") + + async def test_expect_response_should_reject_when_sync_predicate_throws( page: Page, server: Server ) -> None: