Thanks to visit codestin.com
Credit goes to github.com

Skip to content

wait_for raises CancelledError #114496

Closed as not planned
Closed as not planned
@commonism

Description

@commonism

Bug report

Bug description:

Problem is, wait_for sometimes raises CancelledError instead of TimeoutError.
Had a look on recent changes and updated Lib/asyncio/timeouts.py with #113819 by @serhiy-storchaka which seemed similar.

I failed to do to reproduce with sleep only/without httpx.
As the exception raised is either TimeoutError or CancelledError, I consider this an cpython issue.

import pytest
import httpx
import asyncio
import asyncio.timeouts
import random
import logging

@pytest.mark.asyncio
async def test_wait_for():

    async def step() -> bool:
        """
        :return True if finished. False if not stalled
        """
        while True:
            try:
                c = httpx.AsyncClient()
                if False: # works
                    async with asyncio.timeout(6):
                        await asyncio.sleep(random.randrange(1, 5))

                async with c.stream("get", "https://git.kernel.org/torvalds/t/linux-6.8-rc1.tar.gz") as r:
                    async for i in r.aiter_bytes(100):
                        await asyncio.sleep(random.randrange(1, 5))
            except Exception as e:
                logging.exception(e)

    async def install() -> None:
        while True:
            try:
                await asyncio.wait_for(step(), timeout=random.randrange(1,4))
            except (asyncio.TimeoutError, TimeoutError):
                pass
            except asyncio.CancelledError as x:
                logging.exception(x)
                # assert False, "Cancellation"
                raise

    await install()
Traceback (most recent call last):
  File "/tmp/the_test.py", line 576, in install
    await asyncio.wait_for(step(), timeout=random.randrange(1,4))
  File "/usr/lib/python3.12/asyncio/tasks.py", line 520, in wait_for
    return await fut
           ^^^^^^^^^
  File "/tmp/the_test.py", line 569, in step
    await asyncio.sleep(random.randrange(1, 5))
  File "/usr/lib/python3.12/asyncio/tasks.py", line 665, in sleep
    return await future
           ^^^^^^^^^^^^
asyncio.exceptions.CancelledError

CPython versions tested on:

3.12

Operating systems tested on:

Linux

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