Vizonex / Winloop

An Alternative library for uvloop compatability with windows
https://pypi.org/project/winloop
Apache License 2.0
99 stars 11 forks source link

Winloop + Playwright -> startupinfo is not supported #34

Open Haydart opened 2 months ago

Haydart commented 2 months ago

Hi, first of all thank you for doing this project, it's been massively helpful when running my project on Windows. My project used to use Selenium 4.12 for automating browser actions, recently I switched up to Playwright. However, suddenly on Windows I started getting problems. I'm unable to spin up Playwright doe to this crash when Playwright tries to run login in a subprocess:

future: <Task finished name='Task-18' coro=<Connection.run() done, defined at C:\Users\[REDACTED]\venv\Lib\site-packages\playwright\_impl\_connection.py:265> exception=ValueError('startupinfo is not supported')>
Traceback (most recent call last):
  File "C:\Users\[REDACTED]\venv\Lib\site-packages\playwright\_impl\_connection.py", line 272, in run
    await self._transport.connect()
  File "C:\Users\[REDACTED]\venv\Lib\site-packages\playwright\_impl\_transport.py", line 133, in connect
    raise exc
  File "C:\Users\[REDACTED]\venv\Lib\site-packages\playwright\_impl\_transport.py", line 120, in connect
    self._proc = await asyncio.create_subprocess_exec(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\[REDACTED]\AppData\Local\Programs\Python\Python311\Lib\asyncio\subprocess.py", line 223, in create_subprocess_exec
    transport, protocol = await loop.subprocess_exec(
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "winloop\loop.pyx", line 2832, in subprocess_exec
  File "winloop\loop.pyx", line 2773, in __subprocess_run
ValueError: startupinfo is not supported
Traceback (most recent call last):
  File "C:\Users\[REDACTED]\src\tasks\base_task.py", line 125, in execute
    self.browser_context, self.profile = await provide_browser(self.account_id)
                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\[REDACTED]\src\ops\open_browser_session.py", line 118, in open_browser_for_profile
    browser_context = await browser_coro
                      ^^^^^^^^^^^^^^^^^^
  File "C:\Users\[REDACTED]\src\ops\open_browser_session.py", line 98, in get_browser
    playwright = self.__playwright = (await async_playwright().start())
                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\[REDACTED]\venv\Lib\site-packages\playwright\async_api\_context_manager.py", line 51, in start
    return await self.__aenter__()
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\[REDACTED]\venv\Lib\site-packages\playwright\async_api\_context_manager.py", line 46, in __aenter__
    playwright = AsyncPlaywright(next(iter(done)).result())
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\[REDACTED]\venv\Lib\site-packages\playwright\_impl\_transport.py", line 120, in connect
    self._proc = await asyncio.create_subprocess_exec(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\[REDACTED]\AppData\Local\Programs\Python\Python311\Lib\asyncio\subprocess.py", line 223, in create_subprocess_exec
    transport, protocol = await loop.subprocess_exec(
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "winloop\loop.pyx", line 2832, in subprocess_exec
  File "winloop\loop.pyx", line 2773, in __subprocess_run
ValueError: startupinfo is not supported
Process SpawnProcess-1:
Traceback (most recent call last):
  File "C:\Users\[REDACTED]\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 118, in run
Traceback (most recent call last):
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "winloop\loop.pyx", line 1520, in winloop.loop.Loop.run_until_complete
  File "C:\Users\[REDACTED]\AppData\Local\Programs\Python\Python311\Lib\multiprocessing\process.py", line 314, in _bootstrap
    self.run()
  File "C:\Users\[REDACTED]\AppData\Local\Programs\Python\Python311\Lib\multiprocessing\process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\[REDACTED]\AppData\Local\Programs\Python\Python311\Lib\concurrent\futures\process.py", line 249, in _process_worker
    call_item = call_queue.get(block=True)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\[REDACTED]\AppData\Local\Programs\Python\Python311\Lib\multiprocessing\queues.py", line 103, in get
    res = self._recv_bytes()
          ^^^^^^^^^^^^^^^^^^
  File "C:\Users\[REDACTED]\AppData\Local\Programs\Python\Python311\Lib\multiprocessing\connection.py", line 216, in recv_bytes
    buf = self._recv_bytes(maxlength)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\[REDACTED]\AppData\Local\Programs\Python\Python311\Lib\multiprocessing\connection.py", line 321, in _recv_bytes
    waitres = _winapi.WaitForMultipleObjects(
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyboardInterrupt
  File "C:\Users\[REDACTED]\venv\Lib\site-packages\winloop\__init__.py", line 63, in wrapper
    return await main
           ^^^^^^^^^^
  File "C:\Users\[REDACTED]\src\main.py", line 31, in main
    await aio.sleep(1)
  File "C:\Users\[REDACTED]\AppData\Local\Programs\Python\Python311\Lib\asyncio\tasks.py", line 649, in sleep
    return await future
           ^^^^^^^^^^^^
asyncio.exceptions.CancelledError

This is how I currently run my main logic loop:

async def main():
    # async operations
    while True:
        await aio.sleep(1)

if __name__ == "__main__":
    if sys.platform in ('win32', 'cygwin', 'cli'):
        import winloop 
        aio.set_event_loop_policy(winloop.EventLoopPolicy())
        winloop.run(main())
    else:
        from uvloop import run
        run(main())

Is there any way this error can be circumvented / fixed / monkey-patched? I would really like to continue using winloop for this project, the speed of execution improvements are uncanny.

Vizonex commented 2 months ago

@Haydart When it comes to launching a process one of the things I tried to do was that winloop would release the GIL during the initial launch of the process, this was done due to the fact that windows doesn't fork processes and I clearly wanted something that would be it's equal to replace it, perhaps maybe I can look into getting rid of releasing the GIL upon launching the process up first thing and see if it does anything. Otherwise if it isn't our fault after I've made those fixes I would raise this concern to the authors of playwright. I may not be able to fix it right away however due to me being busy with work. But in the meantime Let me give this some careful thinking and consideration. However I also believe that this could be winapi's fault and not my own code that I've written over a year ago. Again glad you brought this issue up for me and the rest of us.

Vizonex commented 2 months ago

@Haydart I'll also see if me or someone else would be willing to write a unittest for playwright specifically to see if that does anything so that this webdriver is supported more in the future.

Vizonex commented 2 months ago

@Haydart I added playwright as part of the testsuite now so hopefully me or one of the other contributors will be able to work on what's causing your problems now.