Skip to content

[Bug]: playwright.stop() deadlocks when used with asyncio-atexit #3004

@Triconsult

Description

@Triconsult

Version

1.56.0

Steps to reproduce

Run this code:

import asyncio

import asyncio_atexit
import playwright.async_api


async def main():
    async_playwright = await playwright.async_api.async_playwright().start()
    asyncio_atexit.register(lambda: stop_playwright(async_playwright))


async def stop_playwright(async_playwright):
    print("Stopping playwright...")
    await async_playwright.stop()  # Deadlock here
    print("Playwright stopped")


asyncio.run(main())

Expected behavior

The process exits.

Actual behavior

The process deadlocks without ever printing "Playwright stopped".

Additional context

I keep running into this problem because I dislike this pattern where everything async needs to be wrapped in an async with, and I try to abstract it away. I know playwright is primarily designed for testing, but I often find myself using it outside of tests as well, and the constant async with playwright(): quickly gets annoying. (And the repeated starting/stopping is very inefficient.) I'd like to keep playwright running until the program/event loop shuts down, but this bug is preventing me from doing that.

Environment

- Operating System: Windows 11
- CPU: AMD Ryzen 5
- Browser: Irrelevant
- Python Version: 3.13
- Other info:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions