microsoft / playwright-pytest

Pytest plugin to write end-to-end browser tests with Playwright.
https://playwright.dev/python/docs/test-runners
Apache License 2.0
433 stars 69 forks source link

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

Open markhobson opened 1 year ago

markhobson commented 1 year ago

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.

mxschmitt commented 1 year ago

Short investigation:

Its hanging here:

https://github.com/microsoft/playwright-python/blob/0e92fbc2cceba0551305f2e36f75f9b71f5d5b6e/playwright/_impl/_connection.py#L276-L277

I was not able to find out the root cause. Since this is the first bug report related to this, I'll mark it as p3-collecting-feedback.