From 2a5c714a61595c4991cc4564dd6d2d642c16e92e Mon Sep 17 00:00:00 2001 From: "microsoft-playwright-automation[bot]" <203992400+microsoft-playwright-automation[bot]@users.noreply.github.com> Date: Thu, 26 Nov 2026 10:01:07 +0000 Subject: [PATCH 1/2] fix(waiter): propagate async predicate cancellation Fixes: https://github.com/microsoft/playwright-python/issues/3379 --- playwright/_impl/waiter.py | 7 ++++++- tests/async/python/test_page.py | 16 +++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/playwright/_impl/waiter.py b/playwright/_impl/waiter.py index 0878dfe79..795f794a5 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,11 @@ def run() -> Any: async def continuation() -> None: try: settle(bool(await matched)) + except asyncio.CancelledError as e: + # Python: settle the wait and remove its listener before + # propagating cancellation of the predicate task. + 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..fab028013 100644 --- a/tests/async/python/test_page.py +++ b/tests/async/python/test_page.py @@ -14,7 +14,8 @@ """Python only: tests of behaviors that no upstream test covers.""" -from typing import Any, AsyncGenerator, Dict +import asyncio +from typing import Any, AsyncGenerator, Dict, Type import pytest @@ -46,18 +47,23 @@ async def test_wait_for_request_should_work_with_predicate( assert request.url == server.PREFIX + "/digits/2.png" +@pytest.mark.parametrize("error_type", [Exception, asyncio.CancelledError]) async def test_expect_response_should_reject_when_async_predicate_throws( - page: Page, server: Server + page: Page, server: Server, error_type: Type[BaseException] ) -> None: + # Regression test for https://github.com/microsoft/playwright-python/issues/3379. await page.goto(server.EMPTY_PAGE) + listener_count = page._impl_obj.listener_count("response") async def predicate(response: Any) -> bool: - raise Exception("Async oops!") + raise error_type("Async oops!") - with pytest.raises(Exception, match="Async oops!"): - async with page.expect_response(predicate): + with pytest.raises(error_type, match="Async oops!"): + async with page.expect_response(predicate, timeout=1_000): await page.evaluate("() => fetch('/digits/1.png')") + assert page._impl_obj.listener_count("response") == listener_count + async def test_expect_response_should_reject_when_sync_predicate_throws( page: Page, server: Server From a1d3be77a2d2631832bd0d9c66b735feee025271 Mon Sep 17 00:00:00 2001 From: "microsoft-playwright-automation[bot]" <203992400+microsoft-playwright-automation[bot]@users.noreply.github.com> Date: Thu, 26 Nov 2026 17:31:06 +0000 Subject: [PATCH 2/2] test(waiter): skip predicate cancellation on Python 3.10 --- tests/async/python/test_page.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tests/async/python/test_page.py b/tests/async/python/test_page.py index fab028013..63cc37278 100644 --- a/tests/async/python/test_page.py +++ b/tests/async/python/test_page.py @@ -15,6 +15,7 @@ """Python only: tests of behaviors that no upstream test covers.""" import asyncio +import sys from typing import Any, AsyncGenerator, Dict, Type import pytest @@ -47,7 +48,19 @@ async def test_wait_for_request_should_work_with_predicate( assert request.url == server.PREFIX + "/digits/2.png" -@pytest.mark.parametrize("error_type", [Exception, asyncio.CancelledError]) +@pytest.mark.parametrize( + "error_type", + [ + Exception, + pytest.param( + asyncio.CancelledError, + marks=pytest.mark.skipif( + sys.version_info < (3, 11), + reason="Python 3.10 does not preserve the CancelledError message", + ), + ), + ], +) async def test_expect_response_should_reject_when_async_predicate_throws( page: Page, server: Server, error_type: Type[BaseException] ) -> None: