Skip to content

[Regression]: pytest.fail() inside an event handler closes the connection #3360

Open

Description

Last Good Version

1.64.0

First Bad Version

1.65.0

Steps to reproduce

import pytest
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    page = p.chromium.launch().new_page()
    page.on("error", lambda e: print("error listener:", e))
    page.on("console", lambda msg: pytest.fail(f"console: {msg.text}"))
    for i in range(2):
        try:
            page.evaluate("console.log('hi')")
            print(i, "ok")
        except BaseException as e:
            print(i, type(e).__name__, str(e).splitlines()[0])

Expected behavior

Same as 1.64:

0 ok
1 Failed console: hi

Actual behavior

0 TargetClosedError Page.evaluate: Target page, context or browser has been closed
1 TargetClosedError Playwright connection closed

The Failed is gone and the connection is dead. The error listener is not called either.

With a plain Exception (e.g. assert False) it still works, so it's only BaseException subclasses — which is what pytest.fail(), pytest.skip() and pytest.xfail() raise. The listener error handling in the dispatch path now catches Exception; before the port it was BaseException.

Additional context

No response

Environment

- Operating System: macOS 27.2
- CPU: arm64
- Browser: Chromium
- Python Version: 3.14
- Other info: sync API

Activity

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

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

    @microsoft-playwright-automation
    Contributor

    Hi, I'm the Playwright bot and I reproduced this.

    Reproduced on current main in Chromium, Firefox, and WebKit; a focused fix is proposed. pytest.fail() escapes both Exception catches in the event path. This stops the pipe reader and closes the connection. The fix sends the outcome to the error listener, or raises it on the next API call if no error listener exists. Cancellation and exit exceptions still pass through.

    The linked PR #3364 also passes the independent repro.

    Repro and verification

    The regression extends test_console_repr. Its unhandled case is:

    def fail_console(message: ConsoleMessage) -> None:
        pytest.fail(f"console: {message.text}")
    
    page.on("console", fail_console)
    page.evaluate("console.log('unhandled')")
    with pytest.raises(pytest.fail.Exception, match="console: unhandled"):
        page.evaluate("1 + 1")
    page.remove_listener("console", fail_console)
    assert page.evaluate("1 + 1") == 2

    The same test first checks that an error listener receives the pytest outcome.

    All runs used macOS 26.7.2, Python 3.14.10, and headless browsers:

    Code Browsers Result
    Main de03c865f5738c2291a6a58c8b5c20346d8e70f2 (1.66.0-next) Chromium, Firefox, WebKit Connection closes; regression fails
    #3364 at 259b19d731b93d1203d5c74a714f33f0e0bfbb09 Chromium, Firefox, WebKit Independent repro and PR tests pass: 6 sync, 3 async cases
    Proposed fix, same driver a018f24a4fc76e2120d3b35d2415f1e5bd4b073e Chromium, Firefox, WebKit Regression passes; connection remains usable

    With the proposed fix, the sync Python and console-event files passed 119 cases; the async listener, lifecycle, cancellation, and console-event files passed 80. Each run had one existing Firefox skip. mypy playwright passed. Full scripts/lint.sh passed using Python 3.13 because the pinned Pyright cannot parse Python 3.14's standard-library t-strings. No source or dependency changes were needed for that lint issue.

    Source history identifies #3280, commit 36d698e1776181c8c7d31bc9fdc32af072cab081, as the change from BaseException to Exception. Its first release tag is v1.65.0. Published v1.64.0 and v1.65.0 packages were not run.

    Draft PR: #3366

    Triaged on macOS 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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions