You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: async_playwright() leaves blocking background task if cancelled quickly #42296
importasynciofromplaywright.async_apiimportasync_playwrightasyncdefrun_browser():
asyncwithasync_playwright():
passasyncdefrun():
task=asyncio.create_task(run_browser())
awaitasyncio.sleep(0.01) # for me: some value between 0.001 and 0.3task.cancel()
try:
awaittaskexceptasyncio.CancelledError:
passasyncio.run(run())
Expected behavior
The program exits.
Actual behavior
The program hangs.
Additional context
No response
Environment
Linux 7.1.3
Python 3.14.7
playwright 1.62.0 (latest version on PyPI)
should/have to be async with async_playwright(): in try block?
No. This raises a CancelledError as expected, which causes the task to be cancelled. The problem is that async_playwright().__aenter__() leaves behind some blocking background task.
The async code doesn't seem easy to understand to me and possibly somewhat brittle.
One thing that immediately stands out: the Python documentation for create_task says:
Save a reference to the result of this function, to avoid a task disappearing mid-execution. The event loop only keeps weak references to tasks. A task that isn’t referenced elsewhere may get garbage collected at any time, even before it’s done.
Cancelling a task that is running a Playwright call is not supported and results in undefined behavior. Documenting this in #42312. If you can reproduce a hang without task.cancel(), please open a new issue.
Cancelling a task that is running a Playwright call is not supported and results in undefined behavior. Documenting this in #42312. If you can reproduce a hang without task.cancel(), please open a new issue.
That would be unfortunate. It would mean that running playwright as a task inside a TaskGroup is undefined behaviour. This is how I found this issue: starting playwright in parallel with some other task that failed.
Activity
sreegithub89 commentedon Aug 18, 2026
should/have to be async with async_playwright(): in try block?
sattlerc commentedon Aug 18, 2026
No. This raises a
CancelledErroras expected, which causes the task to be cancelled. The problem is thatasync_playwright().__aenter__()leaves behind some blocking background task.sreegithub89 commentedon Aug 18, 2026
sattlerc commentedon Aug 19, 2026
That's essentially the same program. Hangs for me.
sattlerc commentedon Aug 19, 2026
The async code doesn't seem easy to understand to me and possibly somewhat brittle.
One thing that immediately stands out: the Python documentation for create_task says:
There seem to be several places where that is not the case. For example, in
PlaywrightContextManager.__aenter__:yury-s commentedon Aug 19, 2026
Cancelling a task that is running a Playwright call is not supported and results in undefined behavior. Documenting this in #42312. If you can reproduce a hang without task.cancel(), please open a new issue.
sattlerc commentedon Aug 19, 2026
That would be unfortunate. It would mean that running playwright as a task inside a TaskGroup is undefined behaviour. This is how I found this issue: starting playwright in parallel with some other task that failed.