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

[Bug]: initializing sync API stuck when using uvloop

Open dittos opened this issue 1 year ago • 1 comments

Version

1.41.2

Steps to reproduce

  1. Clone my repo at https://github.com/dittos/playwright-python-uvloop-bug
  2. pip install -r requirements.txt
  3. DEBUGP=1 python repro.py
  4. You should see the error come up

Expected behavior

get Playwright instance

Actual behavior

get error and blocked

RECV> {
  "id": 1,
  "error": {
    "error": {
      "message": "location: expected object, got null",
      "stack": "Error: location: expected object, got null\n    at /home/ditto/playwright-python-uvloop-bug/.venv/lib/python3.8/site-packages/playwright/driver/package/lib/protocol/validatorPrimitives.js:103:37\n    at /home/ditto/playwright-python-uvloop-bug/.venv/lib/python3.8/site-packages/playwright/driver/package/lib/protocol/validatorPrimitives.js:90:12\n    at /home/ditto/playwright-python-uvloop-bug/.venv/lib/python3.8/site-packages/playwright/driver/package/lib/protocol/validatorPrimitives.js:107:21\n    at /home/ditto/playwright-python-uvloop-bug/.venv/lib/python3.8/site-packages/playwright/driver/package/lib/protocol/validatorPrimitives.js:90:12\n    at DispatcherConnection.dispatch (/home/ditto/playwright-python-uvloop-bug/.venv/lib/python3.8/site-packages/playwright/driver/package/lib/server/dispatchers/dispatcher.js:289:23)\n    at transport.onmessage (/home/ditto/playwright-python-uvloop-bug/.venv/lib/python3.8/site-packages/playwright/driver/package/lib/cli/driver.js:52:57)\n    at Immediate._onImmediate (/home/ditto/playwright-python-uvloop-bug/.venv/lib/python3.8/site-packages/playwright/driver/package/lib/protocol/transport.js:77:34)\n    at process.processImmediate (node:internal/timers:478:21)",
      "name": "Error"
    }
  }
}

Additional context

https://github.com/microsoft/playwright-python/blob/3da0d2b41d9c46e78ab18c8cc86a5caa9bed0741/playwright/_impl/_connection.py#L348

location is None and Playwright node driver does not accept null. However the field is optional so it should be filtered out from the dict if None.

https://github.com/microsoft/playwright/blob/f414227ed8fdeb2735c35a162bebe957cc2951f4/packages/protocol/src/channels.ts#L144-L153

The repro code I provided is minimized version. Situations like this could happen if running FastAPI + Uvicorn + uvloop and using Playwright in sync endpoint

Environment

- Operating System: Ubuntu 20.04 (WSL)
- CPU: x86_64
- Browser: Chromium
- Python Version: 3.8.10

dittos avatar Feb 18 '24 06:02 dittos

@dittos Great writeup. I reproduced the bug and I was able to find a patch as you suggested, see PR.

danphenderson avatar Feb 19 '24 21:02 danphenderson