playwright-python icon indicating copy to clipboard operation
playwright-python copied to clipboard

[Bug]: Using `sync_playwright` to bypass context manager (e.g. in REPL)

Open danphenderson opened this issue 1 year ago • 2 comments

Version

1.41.2

Steps to reproduce

  1. Open the ipython REPL
  2. Create a Playwright instance that bypasses the Python context manager.
from playwright.sync_api import sync_playwright

playwright = sync_playwright().start()
  1. Observe your REPL crash

Expected behavior

I expected it to not crash, and I expected to be able to use the stop method on the playwright instance, as stated in the documentation.

Actual behavior

In [2]: from playwright.sync_api import sync_playwright
   ...: 
   ...: playwright = sync_playwright().start()

Traceback (most recent call last):
  File "/Users/danphenderson/Desktop/open-source/playwright-python/.venv/bin/ipython", line 8, in <module>
    sys.exit(start_ipython())
  File "/Users/danphenderson/Desktop/open-source/playwright-python/.venv/lib/python3.9/site-packages/IPython/__init__.py", line 129, in start_ipython
    return launch_new_instance(argv=argv, **kwargs)
  File "/Users/danphenderson/Desktop/open-source/playwright-python/.venv/lib/python3.9/site-packages/traitlets/config/application.py", line 1075, in launch_instance
    app.start()
  File "/Users/danphenderson/Desktop/open-source/playwright-python/.venv/lib/python3.9/site-packages/IPython/terminal/ipapp.py", line 317, in start
    self.shell.mainloop()
  File "/Users/danphenderson/Desktop/open-source/playwright-python/.venv/lib/python3.9/site-packages/IPython/terminal/interactiveshell.py", line 887, in mainloop
    self.interact()
  File "/Users/danphenderson/Desktop/open-source/playwright-python/.venv/lib/python3.9/site-packages/IPython/terminal/interactiveshell.py", line 872, in interact
    code = self.prompt_for_code()
  File "/Users/danphenderson/Desktop/open-source/playwright-python/.venv/lib/python3.9/site-packages/IPython/terminal/interactiveshell.py", line 813, in prompt_for_code
    text = self.pt_app.prompt(
  File "/Users/danphenderson/Desktop/open-source/playwright-python/.venv/lib/python3.9/site-packages/prompt_toolkit/shortcuts/prompt.py", line 1026, in prompt
    return self.app.run(
  File "/Users/danphenderson/Desktop/open-source/playwright-python/.venv/lib/python3.9/site-packages/prompt_toolkit/application/application.py", line 1002, in run
    return asyncio.run(coro)
  File "/Users/danphenderson/.pyenv/versions/3.9.13/lib/python3.9/asyncio/runners.py", line 33, in run
    raise RuntimeError(
RuntimeError: asyncio.run() cannot be called from a running event loop

If you suspect this is an IPython 8.18.1 bug, please report it at:
    https://github.com/ipython/ipython/issues
or send an email to the mailing list at [email protected]

You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.

Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
    %config Application.verbose_crash=True

sys:1: RuntimeWarning: coroutine 'Application.run_async' was never awaited

Additional context

Stepping into the call stack, the crash occurs when playwright.sync_api._context_manager.py:PlaywrightContextManager.__enter__ executes this line:

        # Switch control to the dispatcher, it'll fire an event and pass control to
        # the calling greenlet.
        self._connection.call_on_object_with_known_name("Playwright", callback_wrapper)

Environment

- Operating System: macOS 12.5
- CPU: M1 Max
- Browser: [All]
- Python Version: [3.11.6, 3.9.13]

danphenderson avatar Feb 13 '24 02:02 danphenderson

This looks like https://github.com/microsoft/playwright-python/issues/2060, unfortunately not much we can do from our side there I guess!

mxschmitt avatar Feb 13 '24 10:02 mxschmitt

Okay, if this isn't an actionable item this issue should address the stop method documentation so that it is correct.

danphenderson avatar Feb 13 '24 18:02 danphenderson

Folding into https://github.com/microsoft/playwright-python/issues/2060.

mxschmitt avatar Feb 26 '24 08:02 mxschmitt