Skip to content

[Bug]: Async event predicate raising CancelledError leaves the event waiter pending #3379

In progress

Description

@himanshug189-sketch

Version

0.1.dev1199+g8b4525108.d20261124

Steps to reproduce

Description

When an asynchronous predicate passed to Playwright's internal wait_for_event() raises asyncio.CancelledError, the event waiter does not settle its promise or remove the registered event listener.

The issue occurs because asyncio.CancelledError inherits from BaseException rather than Exception in modern Python versions. The current exception handler catches only Exception, so the cancellation bypasses the failure handler.

As a result, the event waiter remains pending indefinitely when no timeout is configured, and the event listener remains registered.

Steps to Reproduce

Run the following script from the Playwright Python repository:

import asyncio

from playwright._impl.event_emitter import EventEmitter
from playwright._impl.waiter import wait_for_event
from playwright._impl.utils.zones import empty_zone


async def main():
    emitter = EventEmitter()

    async def predicate(event):
        raise asyncio.CancelledError("Predicate cancelled")

    promise, dispose = wait_for_event(
        emitter, "test", empty_zone, predicate
    )

    emitter.emit("test", "sample event")

    await asyncio.sleep(0.1)

    print("Promise done:", promise.done())
    print("Listener count:", emitter.listener_count("test"))

    dispose()


asyncio.run(main())

Expected behavior

When an asynchronous event predicate raises asyncio.CancelledError:

  • The event waiter should not remain pending indefinitely.
  • The registered event listener should be removed.
  • Cancellation should be propagated appropriately to the caller.

Actual behavior

Output:

Promise done: False
Listener count: 1

Additional context

No response

Environment

- Python: 3.12.3
- Playwright Python repository
- Tested commit: `f31e7025d36bebc4b55c2e94f97f26ac92e13067`
- Branch: `fix-async-predicate-cancelled-error`

Activity

  1. microsoft-playwright-automation commented on Nov 26, 2026

    @microsoft-playwright-automation
  2. microsoft-playwright-automation commented on Nov 26, 2026

    @microsoft-playwright-automation
    Contributor

    Hi, I'm the Playwright bot. I reproduced this and prepared a fix proposal.

    Reproduced on Linux at f31e7025d36bebc4b55c2e94f97f26ac92e13067, with Python 3.12.3. The async predicate task exits on CancelledError, but its waiting future stays pending and its event listener stays registered. The fix removes the listener, passes the original cancellation to the waiter, and re-raises it in the predicate task.

    The proposal extends the existing async-predicate error test with cancellation and listener-cleanup checks.

    Reproduction and checks

    The browser-free check registers an async predicate that raises asyncio.CancelledError("Predicate cancelled"), emits one event, and waits until the predicate has run:

    emitter = EventEmitter()
    entered = asyncio.Event()
    
    async def predicate(event: str) -> bool:
        entered.set()
        raise asyncio.CancelledError("Predicate cancelled")
    
    promise, dispose = wait_for_event(emitter, "sample", empty_zone, predicate)
    emitter.emit("sample", "event")
    await entered.wait()

    Without a timeout, main leaves promise.done() false and one listener registered. With the fix, the future is done, no listener remains, and awaiting the future raises the original cancellation.

    All checks used the driver built from upstream b0e0b7b555e637cc8c0ce94dd98c3be153861333 (1.66.0-next):

    Check Result
    Regression without the fix Cancellation case fails with TimeoutError on Chromium, Firefox and headed WebKit; ordinary exception case passes
    Regression with the fix Both cases pass on headless and headed Chromium/Firefox, and headed WebKit
    Async Python page tests and request/response wait tests Chromium/Firefox: 48 passed; headed WebKit: 22 passed, 2 existing headless-only tests skipped
    Sync request/response wait tests Passed on all three browsers; Chromium/Firefox also passed the Python sync API file: 70 passed combined, plus 15 on WebKit
    Unbounded async predicate rejection in Node.js Rejection propagates and listener is removed
    Linked #3380 at 4d36cf076a253e2ef7ff99f933a95291d00dfd9a Its test and the independent regression pass on Chromium/Firefox: 6 passed; unbounded helper check passes
    mypy playwright, ./scripts/lint.sh, git diff --check Passed

    WebKit initially lacked host libraries. Its checks used libraries extracted under the run's scratch directory, an isolated launcher that preserves their library path, and Xvfb. The system-cache dependency check was disabled after resolving the shared libraries. Headless WebKit remained blocked by a missing EGL display; the Mesa package download returned HTTP 404. Headed WebKit passed. No system packages were changed.

    Draft PR: #3381

    Triaged on Linux by the Playwright bot - agent run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent-triage-done

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions