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

aiohttp.Timeout causes CancelledError #930

Closed
aderbenev opened this issue Jun 17, 2016 · 3 comments
Closed

aiohttp.Timeout causes CancelledError #930

aderbenev opened this issue Jun 17, 2016 · 3 comments
Labels

Comments

@aderbenev
Copy link

Long story short

I use aiohttp.Timeout so that my code could react to connectivity problems and reattempt an unsuccessful connection. When timeout happens, the traceback contains more than that, misleading into a thought that something was prematurely cancelled due to other reasons.

Expected behaviour

Only asyncio.TimeoutError exception is thrown when the specified timeout expires and all other exceptions which result from it are muffled.

Actual behaviour

CancelledError exception is thrown and appears in traceback along with TimeoutError:

Traceback (most recent call last):
  File "timeouttest.py", line 9, in test
    async with session.get("https://www.google.com") as response:
  File "/home/user/test/venv/lib/python3.5/site-packages/aiohttp/client.py", line 539, in __aenter__
    self._resp = yield from self._coro
  File "/home/user/test/venv/lib/python3.5/site-packages/aiohttp/client.py", line 183, in _request
    conn = yield from self._connector.connect(req)
  File "/home/user/test/venv/lib/python3.5/site-packages/aiohttp/connector.py", line 310, in connect
    transport, proto = yield from self._create_connection(req)
  File "/home/user/test/venv/lib/python3.5/site-packages/aiohttp/connector.py", line 581, in _create_connection
    local_addr=self._local_addr)
  File "/home/user/src/python3/lib/python3.5/asyncio/base_events.py", line 599, in create_connection
    yield from tasks.wait(fs, loop=self)
  File "/home/user/src/python3/lib/python3.5/asyncio/tasks.py", line 341, in wait
    return (yield from _wait(fs, timeout, return_when, loop))
  File "/home/user/src/python3/lib/python3.5/asyncio/tasks.py", line 424, in _wait
    yield from waiter
  File "/home/user/src/python3/lib/python3.5/asyncio/futures.py", line 358, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/home/user/src/python3/lib/python3.5/asyncio/tasks.py", line 290, in _wakeup
    future.result()
  File "/home/user/src/python3/lib/python3.5/asyncio/futures.py", line 266, in result
    raise CancelledError
concurrent.futures._base.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "timeouttest.py", line 17, in <module>
    response = asyncio.get_event_loop().run_until_complete(test(timeout))
  File "/home/user/src/python3/lib/python3.5/asyncio/base_events.py", line 337, in run_until_complete
    return future.result()
  File "/home/user/src/python3/lib/python3.5/asyncio/futures.py", line 274, in result
    raise self._exception
  File "/home/user/src/python3/lib/python3.5/asyncio/tasks.py", line 241, in _step
    result = coro.throw(exc)
  File "timeouttest.py", line 10, in test
    return await response.read()
  File "/home/user/test/venv/lib/python3.5/site-packages/aiohttp/helpers.py", line 488, in __exit__
    raise asyncio.TimeoutError
concurrent.futures._base.TimeoutError

Steps to reproduce

Consider the simple sample:

import asyncio
import aiohttp
import traceback

async def test(timeout):
    try:
        with aiohttp.Timeout(timeout):
            with aiohttp.ClientSession() as session:
                async with session.get("https://www.google.com") as response:
                    return await response.read()
    except asyncio.TimeoutError:
        raise

timeout = 0.000001 # To make aiohttp.Timeout trigger

try:
    response = asyncio.get_event_loop().run_until_complete(test(timeout))
    print(response)
except:
    traceback.print_exc()

Your environment

Python 3.5.1
aiohttp (0.21.6)
asyncio (3.4.3)

@asvetlov
Copy link
Member

So you are asking just for suppressing chained exception, isn't it?

@aderbenev
Copy link
Author

If in all cases the appearance of TimeoutError is accompanied by CancelledError, I don't think the latter is all that meaningful to appear. And if I am to catch CancelledError in my code, I'd have to distinguish between it appearing by itself or paired with a timeout. Since a timeout is a legit situation (e.g. connectivity issues), I'd like to receive only the exception associated with it, so yes, suppressing the CancelledError on timeout should solve the issue for me.

@lock
Copy link

lock bot commented Oct 29, 2019

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock bot added the outdated label Oct 29, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants