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

ClientSession.request unexpectedly cancels task on timeout #2286

Closed
savv opened this issue Sep 22, 2017 · 6 comments
Closed

ClientSession.request unexpectedly cancels task on timeout #2286

savv opened this issue Sep 22, 2017 · 6 comments
Labels
Milestone

Comments

@savv
Copy link

savv commented Sep 22, 2017

Long story short

await client.get('https://httpbin.org/delay/10', timeout=.1)
This call throws an exception on timeout, and it cancels the task it's running in.
This makes it hard to handle timeouts locally, e.g. by catching the timeout and returning an alternate value.

It would at least be worth documenting this behavior.

Expected behaviour

I would have liked/expected the sample below to print:
Caught exception! api_req was able to handle timeout.

Actual behaviour

Caught exception! aiohttp cancelled us

Steps to reproduce

import aiohttp
import asyncio

loop = asyncio.get_event_loop()
client = aiohttp.ClientSession(loop=loop)

async def api_req():
    try:
        res = await client.get('https://httpbin.org/delay/10', timeout=.1)
    except asyncio.TimeoutError:
        print('Caught exception!')
        return False
    except:
        print('This should not happen.')
    return True

async def arun():
    done, not_done = await asyncio.wait([api_req()])
    done, = done
    try:
        assert done.result() == False
        print('api_req was able to handle timeout.')
    except asyncio.CancelledError:
        print('aiohttp cancelled us')
    except:
        print('This should not happen.')

loop.run_until_complete(arun())
client.close()
loop.close()

Your environment

python 3.6.2 on osx

@hellysmile
Copy link
Member

can You use just asyncio.TimeourError instead of asyncio.CancelledError?

@savv
Copy link
Author

savv commented Sep 23, 2017

That still won't let me handle the time out within api_req

@savv
Copy link
Author

savv commented Sep 25, 2017

@hellysmile FYI I updated the code snippet to include except: print('This should not happen.'). It still prints exactly the same thing, but now it's clearer which part of the code throws which exception.

One solution would be not to catch the TimeoutError inside of api_req() and let it propagate to arun(), then handle it there. I think this would be a suboptimal solution, because it prevents api_req() from gracefully handling the timeout and returning an alternate value (e.g. a default value).

@wojunnihou
Copy link

wojunnihou commented Sep 26, 2017

I meet the same issues!!!!!
Python 3.5.2 on centos

async def ceshi(request, *args, **kwargs):

    # url = "https://stackoverflow.com/jobs?med=site-ui&ref=jobs-tab"
    url = 'https://api.github.com/events'
    url = 'https://www.baidu.com'
    try:
        async with aiohttp.ClientSession(conn_timeout=5,read_timeout=10) as session:
            async with session.get(url,timeout=5) as resp:
                print(resp.status)
                data = await resp.text()
                return HTTPResponse(data)
    except:
        return HTTPResponse("error", status=400)

@asvetlov asvetlov added this to the 3.0 milestone Oct 19, 2017
@argaen
Copy link
Member

argaen commented Nov 9, 2017

@savvopoulos @wojunnihou I cannot reproduce:

23:04 $ python timeout.py 
Caught exception!
api_req was able to handle timeout.

timeout.py is the script uploaded by @savvopoulos

@lock
Copy link

lock bot commented Oct 28, 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].
[new issue]: https://github.com/aio-libs/aiohttp/issues/new

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 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

5 participants