[Regression]: pytest.fail() inside an event handler closes the connection
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 65/100
Research direction
Run the reproduction script from the report against a local checkout to confirm the current failure, then look at where the sync API dispatches listener callbacks and where a listener's exception is caught. The report narrows it to that catch clause: it handles Exception, and it needs to cover BaseException subclasses such as the ones pytest.fail(), pytest.skip() and pytest.xfail() raise, while still not killing the connection. Done means the run prints 1 Failed console: hi, the error listener still fires, and the page stays usable afterwards; add a regression test next to the existing listener-error tests.
Written by the indexing model from the issue text.
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
- Dominant language
- Python
- Stars
- 15.1k
- Forks
- 1.3k
- Avg merge
- 6h 16m
- Merged PRs (30d)
- 53
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from microsoft/playwright-python
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
microsoft/playwright-python#3361 ·
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 62/100
microsoft/playwright-python#3362 ·
Maintainers usually reply within 1 day
All issues in microsoft/playwright-python
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 88/100
snap-stanford/Biomni#380 ·
-
documentation priority: low size: S status: needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
cubrid-lab/sqlalchemy-cubrid#1397 ·
Maintainers usually reply within 1 day
-
documentation priority: low size: S status: needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
cubrid-lab/pycubrid#1307 ·
Maintainers usually reply within 1 day
-
[Bug]: Installer hides SSL certificate errors as bare URLError, and the suggested fix can't workOpenbug
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
ShaftHQ/chaosengine-harness#143 ·
Maintainers usually reply within 1 day
-
bug high-severity
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
aws/bedrock-agentcore-sdk-python#752 ·
Maintainers usually reply within 1 day