From 218b0b75da74bc4e025bc5707d96af1d4f817e05 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Wed, 19 Aug 2026 11:17:03 -0700 Subject: [PATCH 1/2] docs(python): document that task cancellation is not supported Cancellation is forwarded to the driver, but the connection is not guaranteed to be usable afterwards, and cancelling during startup can leave the driver process running. --- docs/src/library-python.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/src/library-python.md b/docs/src/library-python.md index 9dcf0cba99e10..92dbb939c63d4 100644 --- a/docs/src/library-python.md +++ b/docs/src/library-python.md @@ -199,3 +199,7 @@ On Windows Python 3.7, Playwright sets the default event loop to `ProactorEventL ### Threading Playwright's API is not thread-safe. If you are using Playwright in a multi-threaded environment, you should create a playwright instance per thread. See [threading issue](https://github.com/microsoft/playwright-python/issues/623) for more details. + +### Cancelling `asyncio` tasks + +Cancelling a task that is running a Playwright call is not supported. Playwright forwards the cancellation to the driver on a best-effort basis, but it does not guarantee that the connection is left in a usable state afterwards. Cancelling a task while `async_playwright()` is still starting up can leave the driver process running. If an operation has to outlive its caller, run it in a separate task and protect it with [`asyncio.shield()`](https://docs.python.org/3/library/asyncio-task.html#asyncio.shield). From 4640b4ea9350cd7474fd4f897818c8b431bf20e1 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Wed, 19 Aug 2026 11:18:03 -0700 Subject: [PATCH 2/2] docs(python): simplify the task cancellation clause --- docs/src/library-python.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/library-python.md b/docs/src/library-python.md index 92dbb939c63d4..6ec0eeb1db998 100644 --- a/docs/src/library-python.md +++ b/docs/src/library-python.md @@ -202,4 +202,4 @@ Playwright's API is not thread-safe. If you are using Playwright in a multi-thre ### Cancelling `asyncio` tasks -Cancelling a task that is running a Playwright call is not supported. Playwright forwards the cancellation to the driver on a best-effort basis, but it does not guarantee that the connection is left in a usable state afterwards. Cancelling a task while `async_playwright()` is still starting up can leave the driver process running. If an operation has to outlive its caller, run it in a separate task and protect it with [`asyncio.shield()`](https://docs.python.org/3/library/asyncio-task.html#asyncio.shield). +Cancelling a task that is running a Playwright call is not supported and results in undefined behavior. If an operation has to outlive its caller, run it in a separate task and protect it with [`asyncio.shield()`](https://docs.python.org/3/library/asyncio-task.html#asyncio.shield).