Skip to content

fix: handle cancelled tasks #536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

cruzdanilo
Copy link

prevents InvalidStateError when the task has been cancelled, here:

callback.future.set_exception(parsed_error)

@pavelfeldman
Copy link
Member

Do you have a reproduction for this bug? callback.future.set_exception and callback.future.set_result are in different branches of if, they can't both happen.

@cruzdanilo
Copy link
Author

from asyncio import FIRST_COMPLETED, CancelledError, create_task, run, wait
from contextlib import suppress
from playwright.async_api import async_playwright

async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch()
        page = await browser.new_page()
        await page.goto("http://playwright.dev")
        done, pending = await wait({
            create_task(page.wait_for_selector("input[placeholder='search' i]")),
            create_task(page.wait_for_selector("input[placeholder='will-never-find' i]")),
        }, return_when=FIRST_COMPLETED)
        for task in pending:
            task.cancel()
            with suppress(CancelledError):
                await task

        await browser.close()

run(main())
Task exception was never retrieved
future: <Task finished name='Task-2' coro=<Connection.run() done, defined at lib/python3.9/site-packages/playwright/_impl/_connection.py:163> exception=InvalidStateError('invalid state')>
Traceback (most recent call last):
  File "lib/python3.9/site-packages/playwright/_impl/_connection.py", line 166, in run
    await self._transport.run()
  File "lib/python3.9/site-packages/playwright/_impl/_transport.py", line 90, in run
    self.on_message(obj)
  File "lib/python3.9/site-packages/playwright/_impl/_connection.py", line 150, in <lambda>
    self._transport.on_message = lambda msg: self._dispatch(msg)
  File "lib/python3.9/site-packages/playwright/_impl/_connection.py", line 229, in _dispatch
    callback.future.set_exception(parsed_error)
asyncio.exceptions.InvalidStateError: invalid state

@pavelfeldman
Copy link
Member

Got it, I did not realize the task was canceled externally. Would you mind adding this as a test for it?

@mxschmitt
Copy link
Member

I picked it up in #624 so it lands earlier. Thanks for the repo and explanation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants