Skip to content

[BUG] Task was destroyed but it is pending! in route.abort() #1457

@x0day

Description

@x0day

Context:

  • Playwright Version: 1.24.0
  • Operating System: Mac

Code Snippet

import asyncio
import logging
import random

from playwright.async_api import Route, async_playwright


target = "https://www.baidu.com/"

async def on_request(route: Route):
    if route.request.url == target:
        return await route.continue_()

    # random abort
    if random.random() < 0.7:
        await route.continue_()
    else:
        print(f'aborted {route.request.url}')
        await route.abort()


async def main():
    async with async_playwright() as playwright:
        browser = await playwright.chromium.launch()
        context = await browser.new_context()

        page = await context.new_page()

        await page.route("*/**", on_request)
        await page.goto(target)

        await page.close()
        await context.close()
        await browser.close()

        for task in asyncio.all_tasks(browser._loop):
            if not (task.done() or task.cancelled()):
                print(task)


if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    asyncio.run(main())

output

aborted https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/topnav/newxueshuicon-a5314d5c83.png
aborted https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/topnav/newwenku-d8c9b7b0fb.png
aborted https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/topnav/newbaike-889054f349.png
aborted https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/topnav/newyinyue-03ecd1e9b9.png
aborted https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png
aborted https://www.baidu.com/img/PCfb_5bf082d29588c07f842ccde3f97243ea.png
aborted https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/qrcode/qrcode@2x-daf987ad02.png
aborted https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/topnav/newxueshuicon-a5314d5c83.png
aborted https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/topnav/newwenku-d8c9b7b0fb.png
aborted https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/topnav/newbaike-889054f349.png
aborted https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/topnav/newyinyue-03ecd1e9b9.png
aborted https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png
aborted https://www.baidu.com/img/PCfb_5bf082d29588c07f842ccde3f97243ea.png
aborted https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/js/sbase-57dfcf5eeb.js
aborted https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/css/ubase_sync-d600f57804.css?v=md5
aborted https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/js/s_super_index-3fffae8d60.js
aborted https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/js/components/hotsearch-3ac31d4e9b.js
aborted https://www.baidu.com/img/flexible/logo/pc/index@2.png
aborted https://pss.bdstatic.com/r/www/cache/static/protocol/https/plugins/bzPopper_7bc4f0e.js
aborted https://pss.bdstatic.com/r/www/cache/static/protocol/https/soutu/js/tu_ce72dbc.js
aborted https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/searchbox/nicon-10750f3f7d.png
<Task pending name='Task-128' coro=<Page._on_route() running at /Users/dev/Library/Caches/pypoetry/virtualenvs/playwright-remote-5hR_76HL-py3.8/lib/python3.8/site-packages/playwright/_impl/_page.py:246> wait_for=<_GatheringFuture pending cb=[<TaskWakeupMethWrapper object at 0x7fb2c1ca7250>()]> cb=[AsyncIOEventEmitter._emit_run.<locals>._callback() at /Users/dev/Library/Caches/pypoetry/virtualenvs/playwright-remote-5hR_76HL-py3.8/lib/python3.8/site-packages/pyee/_asyncio.py:55]>
.. many many warning here ..
<Task pending name='Task-119' coro=<Page._on_route() running at /Users/dev/Library/Caches/pypoetry/virtualenvs/playwright-remote-5hR_76HL-py3.8/lib/python3.8/site-packages/playwright/_impl/_page.py:246> wait_for=<_GatheringFuture pending cb=[<TaskWakeupMethWrapper object at 0x7fb2c1f705b0>()]> cb=[AsyncIOEventEmitter._emit_run.<locals>._callback() at /Users/dev/Library/Caches/pypoetry/virtualenvs/playwright-remote-5hR_76HL-py3.8/lib/python3.8/site-packages/pyee/_asyncio.py:55]>

Describe the bug

Route._start_handling future can't gather in RouteHandler, because route abort not set the _handling_future result.

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