Skip to content

wait_for eats cancelled error if the future is already complete #107547

Closed as not planned
@zevbo

Description

@zevbo

Bug report

If the future asyncio.wait_for is waiting on has already completed, and the task running wait_for gets cancelled, wait_for will eat the asyncio.CancelledError, and carry on as if nothing is happening.

Checklist

  • I am confident this is a bug in CPython, not a bug in a third-party project
  • I have searched the CPython issue tracker, and am confident this bug has not been reported before

A clear and concise description of the bug

import asyncio

async def simple_task() -> None:
  await asyncio.sleep(0.0)

async def run_task() -> None:
  task = asyncio.create_task(simple_task())
  await task  # Task is done
  asyncio.current_task().cancel() # Cancel as soon as we're in wait for
  await asyncio.wait_for(task, timeout=10)  # Doesn't raise a cancelled erorr
  await asyncio.sleep(0.01)  # Doesn't raise a cancelled erorr

async def main() -> None:
  await asyncio.create_task(run_task())  # Doesn't raise a cancelled erorr

asyncio.run(main())

Notably, if you remove the await task line, the cancelled error gets raised.

Note that this may have been fixed in the following PR, that deleted the highlighted (I think problematic but I could be wrong) 3 lines of code:
https://github.com/python/cpython/pull/98518/files#diff-429f4ed1e0f89ea2c92e2a8e8548ea8ae1a3d528979554fbfa4c38329e951529L470-L472
But, I couldn't verify this as I don't think it got into 3.11.4, so I can't really run with it.

Your environment

  • CPython versions tested on: python 3.10 [I see the same behavior in 3.11.4 as well]
  • Operating system and architecture: Ubuntu 22.04

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingThe issue will be closed if no feedback is providedtopic-asynciotype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions