Skip to content

fix: handle cancellation in async event predicates - #3380

Closed
himanshug189-sketch wants to merge 1 commit into
microsoft:mainfrom
himanshug189-sketch:fix-async-predicate-cancelled-error
Closed

himanshug189-sketch wants to merge 1 commit into
microsoft:mainfrom
himanshug189-sketch:fix-async-predicate-cancelled-error

Conversation

@himanshug189-sketch

@himanshug189-sketch himanshug189-sketch commented Nov 26, 2026 •

Copy link
Copy Markdown

Summary

Fixes async event predicate cancellation handling so that asyncio.CancelledError is propagated correctly and the associated event listener is cleaned up.

Problem

When an asynchronous predicate passed to an event-waiting API such as page.expect_response() raises asyncio.CancelledError, the existing implementation does not handle it correctly.

The async predicate continuation catches Exception, but asyncio.CancelledError inherits from BaseException. As a result, it bypasses the normal failure handler, potentially leaving the event waiter's promise unresolved and causing the expectation to hang until timeout.

Reproduction

The regression test reproduces the issue using the following steps:

  1. Navigate to server.EMPTY_PAGE.
  2. Define an asynchronous response predicate that raises asyncio.CancelledError.
  3. Start page.expect_response(predicate).
  4. Trigger a response using fetch('/digits/1.png').
  5. Verify that cancellation propagates and the response listener is removed.

Expected Behaviour

  • asyncio.CancelledError should propagate to the caller.
  • The event waiter should not remain pending.
  • The associated response listener should be cleaned up.

Implementation

  • Updated the failure handler to accept BaseException.
  • Explicitly handle asyncio.CancelledError in the asynchronous predicate continuation.
  • Settle the waiter with the cancellation exception and re-raise it to preserve asyncio cancellation semantics.
  • Added a regression assertion to verify that the response listener count returns to its original value after cancellation.

Verification

Executed:

python -m pytest tests/async/python/test_page.py -k "async_predicate" --browser chromium -q

Result: 2 passed, 7 deselected on Python 3.12.3.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@himanshug189-sketch

Copy link
Copy Markdown
Author

Hi,

I've updated the PR description with a clearer explanation of the root cause, reproduction steps, expected behaviour, implementation details, and test verification.

I've also strengthened the regression test to verify that the response event listener is cleaned up after an async predicate raises asyncio.CancelledError. The targeted tests are passing.

I've pushed the latest changes to the same branch.

Could you please consider reopening this PR so I can continue addressing any review feedback?

I understand there may be overlapping work on the same issue. I'm happy to coordinate and avoid duplicate changes.

Thanks!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants