microsoft / microsoft/playwright-pytest

[BUG] Playwright fixture hangs on teardown with pytest-flask

Open
#187 8 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

p3-collecting-feedback
Dominant language
Python
Stars
563
Forks
90
Avg merge
11m
Merged PRs (30d)
1

Description

When using playwright-pytest in conjunction with pytest-flask the playwright fixture can hang on teardown.

See markhobson/playwright-pytest-bug for a reproducible example.

Notice the two tests and their fixtures:

def test_page(page: Page):
def test_server_and_page(live_server: LiveServer, page: Page):

Running these tests results in the following output:

$ pytest --setup-show
========================================================================== test session starts ==========================================================================
platform linux -- Python 3.10.12, pytest-7.4.2, pluggy-1.3.0
rootdir: /home/mark/Projects/mark/playwright-pytest-bug
plugins: flask-1.2.0, base-url-2.0.0, playwright-0.4.2
collected 2 items                                                                                                                                                       

tests/test_browser.py 
SETUP    S base_url
SETUP    S _verify_url (fixtures used: base_url)
SETUP    S pytestconfig
SETUP    S delete_output_dir (fixtures used: pytestconfig)
SETUP    S browser_type_launch_args (fixtures used: pytestconfig)
SETUP    S playwright
SETUP    S browser_name['chromium']
SETUP    S browser_type (fixtures used: browser_name, playwright)
SETUP    S launch_browser (fixtures used: browser_type, browser_type_launch_args)
SETUP    S browser (fixtures used: launch_browser)
SETUP    S device (fixtures used: pytestconfig)
SETUP    S browser_context_args (fixtures used: base_url, device, playwright, pytestconfig)
        SETUP    F monkeypatch
        SETUP    F _configure_application (fixtures used: monkeypatch)
        SETUP    F _monkeypatch_response_class (fixtures used: monkeypatch)
        SETUP    F _push_request_context
        SETUP    F context (fixtures used: browser, browser_context_args, pytestconfig)
        SETUP    F page (fixtures used: context)
        tests/test_browser.py::test_page[chromium] (fixtures used: _configure_application, _monkeypatch_response_class, _push_request_context, _verify_url, base_url, browser, browser_context_args, browser_name, browser_type, browser_type_launch_args, context, delete_output_dir, device, launch_browser, monkeypatch, page, playwright, pytestconfig, request).
        TEARDOWN F page
        TEARDOWN F context
        TEARDOWN F _push_request_context
        TEARDOWN F _monkeypatch_response_class
        TEARDOWN F _configure_application
        TEARDOWN F monkeypatch
SETUP    S app
SETUP    S live_server (fixtures used: app, pytestconfig)
        SETUP    F monkeypatch
        SETUP    F _configure_application (fixtures used: monkeypatch)
        SETUP    F _monkeypatch_response_class (fixtures used: monkeypatch)
        SETUP    F _push_request_context
        SETUP    F context (fixtures used: browser, browser_context_args, pytestconfig)
        SETUP    F page (fixtures used: context)
        tests/test_browser.py::test_server_and_page[chromium] (fixtures used: _configure_application, _monkeypatch_response_class, _push_request_context, _verify_url, app, base_url, browser, browser_context_args, browser_name, browser_type, browser_type_launch_args, context, delete_output_dir, device, launch_browser, live_server, monkeypatch, page, playwright, pytestconfig, request).
        TEARDOWN F page
        TEARDOWN F context
        TEARDOWN F _push_request_context
        TEARDOWN F _monkeypatch_response_class
        TEARDOWN F _configure_application
        TEARDOWN F monkeypatch
TEARDOWN S browser_context_args
TEARDOWN S device
TEARDOWN S browser
TEARDOWN S launch_browser
TEARDOWN S browser_type
TEARDOWN S browser_name['chromium']

It then hangs. After CTRL+C it terminates with:

^C
TEARDOWN S playwright

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! KeyboardInterrupt !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
/usr/lib/python3.10/selectors.py:469: KeyboardInterrupt
(to show a full traceback on KeyboardInterrupt use --full-trace)
========================================================================== 2 passed in 11.52s ===========================================================================
Exception ignored in: <function BaseSubprocessTransport.__del__ at 0x7f92cc28dfc0>
Traceback (most recent call last):
  File "/usr/lib/python3.10/asyncio/base_subprocess.py", line 126, in __del__
  File "/usr/lib/python3.10/asyncio/base_subprocess.py", line 104, in close
  File "/usr/lib/python3.10/asyncio/unix_events.py", line 547, in close
  File "/usr/lib/python3.10/asyncio/unix_events.py", line 571, in _close
  File "/usr/lib/python3.10/asyncio/base_events.py", line 753, in call_soon
  File "/usr/lib/python3.10/asyncio/base_events.py", line 515, in _check_closed
RuntimeError: Event loop is closed

The order of tests and fixtures appears significant. Injecting both fixtures into both tests is fine, as is injecting just page into both tests.

As an interaction between pytest-flask and playwright-pytest it's hard to pinpoint in which project the problem lies, but as it's the teardown of the playwright fixture that is hanging, I've raised this issue here.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducible example in markhobson/playwright-pytest-bug and its tests/test_browser.py fixtures. Run pytest --setup-show and compare the fixture teardown order when using page alone versus page with live_server. Trace the playwright fixture teardown after the browser fixtures finish; done means the two-test combination exits without hanging and preserves the existing passing behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
playwright, python
Domain
devtools, testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.