Skip to content

[Bug]: Failed async startup leaves Connection.init task exception unretrieved #3132

Description

@karmas056

Version

1.61.0 (latest release)

Steps to reproduce

Minimal reproduction: https://github.com/karmas056/playwright-init-task-repro

  1. Clone the repository:

    git clone https://github.com/karmas056/playwright-init-task-repro.git
    cd playwright-init-task-repro
  2. Build and run it:

    docker build -t playwright-init-task-repro .
    docker run --rm playwright-init-task-repro

The reproduction injects a minimal Transport that deterministically closes
while Connection.init is pending. It does not launch a browser or a real
Playwright Node driver, so it isolates the Python startup cleanup path from
browser and operating-system behavior.

The script exits successfully only after the event loop observes an unconsumed
Connection.run.<locals>.init task.

Expected behavior

The transport/startup exception should be raised to the caller, and every
internal task created by the failed startup attempt should be awaited,
cancelled, or have its exception consumed.

Garbage collection should not produce a secondary
Task exception was never retrieved warning.

Actual behavior

The original startup exception is raised to the caller, but after gc.collect()
the event loop reports an unconsumed internal init task:

startup_error=Exception: Connection closed while reading from the driver (forced repro)
result=REPRODUCED
task=Connection.run.<locals>.init
unretrieved_exception=Exception: Connection.init: Connection closed while reading from the driver (forced repro)
Task exception was never retrieved
future: <Task finished name='Task-3' coro=<Connection.run.<locals>.init() ...>

The relevant traceback ends with:

File "playwright/_impl/_connection.py", line 310, in init
    self.playwright_future.set_result(await self._root_object.initialize())
...
Exception: Connection.init: Connection closed while reading from the driver (forced repro)

Additional context

This was initially observed after a real, intermittent driver disconnect in a
long-running asyncio scheduler. A startup attempt logged
Connection closed while reading from the driver, retried successfully, and a
later garbage-collection cycle reported the abandoned init-task exception.

The deterministic reproduction points to the following startup path:

I searched the existing issues. #2091 has a related unhandled
Connection.run() startup task, and #650 covers transport errors during
context-manager initialization, but I did not find an issue covering this
completed _init_task cleanup path.

Environment

- Operating System: Debian GNU/Linux 13 (trixie), Docker
- CPU: x86_64
- Browser: N/A (the failure and reproduction occur before browser launch)
- Python Version: 3.12.13
- Playwright Version: 1.61.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions