Skip to content
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

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

Closed
2 tasks done
zevbo opened this issue Aug 1, 2023 · 2 comments
Closed
2 tasks done

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

zevbo opened this issue Aug 1, 2023 · 2 comments
Labels
pending The issue will be closed if no feedback is provided topic-asyncio type-bug An unexpected behavior, bug, or error

Comments

@zevbo
Copy link

zevbo commented Aug 1, 2023

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:/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
@zevbo zevbo added the type-bug An unexpected behavior, bug, or error label Aug 1, 2023
@gvanrossum
Copy link
Member

@kumaraditya303

@kumaraditya303
Copy link
Contributor

It is fixed on 3.12 onwards as done in #96764, we have decided to not fix it in older branches as it too risky to introduce more regressions, you can use asyncio.timeout in 3.11 or async_timeout package for older versions.

@kumaraditya303 kumaraditya303 added the pending The issue will be closed if no feedback is provided label Aug 2, 2023
@kumaraditya303 kumaraditya303 closed this as not planned Won't fix, can't repro, duplicate, stale Aug 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending The issue will be closed if no feedback is provided topic-asyncio type-bug An unexpected behavior, bug, or error
Projects
Status: Done
Development

No branches or pull requests

4 participants