[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 callback,以及 listener 的 exception 在哪裡被捕捉。報告將其縮小到那個 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 摘要。