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

未关闭 适合新手
#3,360 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
65/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
backend

调研方向

针对本地 checkout 运行报告中的复现脚本,以确认当前的失败,然后查看 sync API 在哪里分发 listener 回调,以及 listener 的异常在哪里被捕获。报告将其缩小到那个 catch 子句:它处理 Exception,并且需要覆盖 BaseException 的子类,例如 pytest.fail()、pytest.skip() 和 pytest.xfail() 抛出的那些,同时仍然不杀死连接。完成意味着运行会打印 1 Failed console: hi,error listener 仍会触发,并且之后页面仍然可用;在现有 listener-error 测试旁边添加一个回归测试。

由索引模型根据 Issue 内容生成。

描述

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
主要语言
Python
星标
15.1k
派生
1.3k
平均合并
6 小时 16 分钟
30 天内合并 PR
53

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

microsoft/playwright-python 的其他 Issue

查看 microsoft/playwright-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。