Skip to content

[BUG] Sync API functions expecting to be executed in a asyncio task #1263

@vEpiphyte

Description

@vEpiphyte

Context:

  • Playwright Version: 1.21.0
  • Operating System: Linux / Ubuntu
  • Python version: 3.8.x
  • Browser: Chromium (presumably All would be affected)
  • Extra: Unit test execution

Code Snippet

I don't have a small reproducible snippet, but could come up with one if needed. Here is a example traceback that was encountered when running tests.


__________________________________________________ SomeTest.test_stuff_async ___________________________________________________
../../.pyenv/versions/3.8.10/envs/project3810/lib/python3.8/site-packages/package00/tests/test_playwright/common.py:89: in setUp
    self.page.set_default_timeout(20000)
../../.pyenv/versions/3.8.10/envs/project3810/lib/python3.8/site-packages/playwright/async_api/_generated.py:6609: in set_default_timeout
    self._impl_obj.set_default_timeout(timeout=timeout)
../../.pyenv/versions/3.8.10/envs/project3810/lib/python3.8/site-packages/playwright/_impl/_page.py:347: in set_default_timeout
    self._channel.send_no_reply("setDefaultTimeoutNoReply", dict(timeout=timeout))
../../.pyenv/versions/3.8.10/envs/project3810/lib/python3.8/site-packages/playwright/_impl/_connection.py:80: in send_no_reply
    self._connection._send_message_to_server(self._guid, method, params)
../../.pyenv/versions/3.8.10/envs/project3810/lib/python3.8/site-packages/playwright/_impl/_connection.py:227: in _send_message_to_server
    callback = ProtocolCallback(self._loop)
../../.pyenv/versions/3.8.10/envs/project3810/lib/python3.8/site-packages/playwright/_impl/_connection.py:128: in __init__
    current_task = asyncio.current_task()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

loop = None

    def current_task(loop=None):
        """Return a currently executed task."""
        if loop is None:
>           loop = events.get_running_loop()
E           RuntimeError: no running event loop

../../.pyenv/versions/3.8.10/lib/python3.8/asyncio/tasks.py:37: RuntimeError

Describe the bug

With the changes in #1236 the ProtocolCallback.init method now expects to always be called from inside of an async task [due to the use of asyncio.current_task()].

There are a handful of APIs which are not async functions and previously worked when executed outside of an async task, such as Page.set_default_timeout which can be found here https://github.com/microsoft/playwright-python/blob/v1.21.0/playwright/_impl/_page.py#L345

My coworkers and I stumbled across this due to unit test failures occurring inside of Python Unittest.TestCase.setUp() methods where the playwright context and new page where explicitly being handled by an ioloop constructed for the test, but the set_default_timeout() call was being called directly from the mainthread. Since there was no running ioloop in the main thread, the use of asyncio.current_task() raises an exception.

I'm not certain if there is a direct fix here other than to "Do all playwright related activities in the thread it was made" which is fine for us, but wanted to record this in the event that other users stumbled across this issue, since code which previously worked with 1.20.x could now break.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions