question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RuntimeError: Timeout context manager should be used inside a task

See original GitHub issue

So, basically I want to use starlette (https://www.starlette.io/) as a webhook for telegram bot and iogram as a way to handle bot messages.

I’ve created an endpoint to get the telegram bot updates:

class UpdateTelegramBotEndpoint(HTTPEndpoint):
    async def post(self, request):
        update_data = await request.json()
        await process_update(update_data)
        return JSONResponse(update_data, status_code=200)

The process_update function (I actually don’t know whether it is right to use .set_current() methods, but that’s is not a point):

async def process_update(update_data: dict):
    update = Update(**update_data)

    Bot.set_current(bot)
    Dispatcher.set_current(dispatcher)

    return await dispatcher.process_update(update)

Message handler function:

bot = Bot(token=settings.TELEGRAM_API_KEY)
dispatcher = Dispatcher(bot)


@dispatcher.message_handler(commands=('start', 'help'))
async def send_help_message(message):
    await message.reply(
        '*NoteBot welcomes you!*\n\n'
        'Forward me a message from a channel to save it as Evernote note. '
        'Admit that you should grant access for me to be able to create'
        'notes for you.\n\n'
        '*Commands*:\n'
        '\t/login - Login into Evernote',
        parse_mode='Markdown'
    )

When I’m trying to send bot a /help message, I’m getting such error:

INFO:     91.108.6.40:0 - "POST /updates/ HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/home/vitaliy/.cache/pypoetry/virtualenvs/telegram-evernote-bot-IC7bT7Nj-py3.8/lib/python3.8/site-packages/uvicorn/protocols/http/httptools_impl.py", line 385, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/home/vitaliy/.cache/pypoetry/virtualenvs/telegram-evernote-bot-IC7bT7Nj-py3.8/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
    return await self.app(scope, receive, send)
  File "/home/vitaliy/.cache/pypoetry/virtualenvs/telegram-evernote-bot-IC7bT7Nj-py3.8/lib/python3.8/site-packages/starlette/applications.py", line 102, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/home/vitaliy/.cache/pypoetry/virtualenvs/telegram-evernote-bot-IC7bT7Nj-py3.8/lib/python3.8/site-packages/starlette/middleware/errors.py", line 181, in __call__
    raise exc from None
  File "/home/vitaliy/.cache/pypoetry/virtualenvs/telegram-evernote-bot-IC7bT7Nj-py3.8/lib/python3.8/site-packages/starlette/middleware/errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "/home/vitaliy/.cache/pypoetry/virtualenvs/telegram-evernote-bot-IC7bT7Nj-py3.8/lib/python3.8/site-packages/starlette/exceptions.py", line 82, in __call__
    raise exc from None
  File "/home/vitaliy/.cache/pypoetry/virtualenvs/telegram-evernote-bot-IC7bT7Nj-py3.8/lib/python3.8/site-packages/starlette/exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "/home/vitaliy/.cache/pypoetry/virtualenvs/telegram-evernote-bot-IC7bT7Nj-py3.8/lib/python3.8/site-packages/starlette/routing.py", line 550, in __call__
    await route.handle(scope, receive, send)
  File "/home/vitaliy/.cache/pypoetry/virtualenvs/telegram-evernote-bot-IC7bT7Nj-py3.8/lib/python3.8/site-packages/starlette/routing.py", line 227, in handle
    await self.app(scope, receive, send)
  File "/home/vitaliy/.cache/pypoetry/virtualenvs/telegram-evernote-bot-IC7bT7Nj-py3.8/lib/python3.8/site-packages/starlette/endpoints.py", line 30, in dispatch
    response = await handler(request)
  File "/home/vitaliy/coding/telegram-evernote-bot/src/views.py", line 57, in post
    await process_update(update_data)
  File "/home/vitaliy/coding/telegram-evernote-bot/src/services/updates.py", line 13, in process_update
    return await dispatcher.process_update(update)
  File "/home/vitaliy/.cache/pypoetry/virtualenvs/telegram-evernote-bot-IC7bT7Nj-py3.8/lib/python3.8/site-packages/aiogram/dispatcher/dispatcher.py", line 214, in process_update
    return await self.message_handlers.notify(update.message)
  File "/home/vitaliy/.cache/pypoetry/virtualenvs/telegram-evernote-bot-IC7bT7Nj-py3.8/lib/python3.8/site-packages/aiogram/dispatcher/handler.py", line 117, in notify
    response = await handler_obj.handler(*args, **partial_data)
  File "/home/vitaliy/coding/telegram-evernote-bot/src/bot.py", line 21, in send_help_message
    await message.reply(
  File "/home/vitaliy/.cache/pypoetry/virtualenvs/telegram-evernote-bot-IC7bT7Nj-py3.8/lib/python3.8/site-packages/aiogram/types/message.py", line 995, in reply
    return await self.bot.send_message(
  File "/home/vitaliy/.cache/pypoetry/virtualenvs/telegram-evernote-bot-IC7bT7Nj-py3.8/lib/python3.8/site-packages/aiogram/bot/bot.py", line 219, in send_message
    result = await self.request(api.Methods.SEND_MESSAGE, payload)
  File "/home/vitaliy/.cache/pypoetry/virtualenvs/telegram-evernote-bot-IC7bT7Nj-py3.8/lib/python3.8/site-packages/aiogram/bot/base.py", line 201, in request
    return await api.make_request(self.session, self.__token, method, data, files,
  File "/home/vitaliy/.cache/pypoetry/virtualenvs/telegram-evernote-bot-IC7bT7Nj-py3.8/lib/python3.8/site-packages/aiogram/bot/api.py", line 103, in make_request
    async with session.post(url, data=req, **kwargs) as response:
  File "/home/vitaliy/.cache/pypoetry/virtualenvs/telegram-evernote-bot-IC7bT7Nj-py3.8/lib/python3.8/site-packages/aiohttp/client.py", line 1012, in __aenter__
    self._resp = await self._coro
  File "/home/vitaliy/.cache/pypoetry/virtualenvs/telegram-evernote-bot-IC7bT7Nj-py3.8/lib/python3.8/site-packages/aiohttp/client.py", line 426, in _request
    with timer:
  File "/home/vitaliy/.cache/pypoetry/virtualenvs/telegram-evernote-bot-IC7bT7Nj-py3.8/lib/python3.8/site-packages/aiohttp/helpers.py", line 579, in __enter__
    raise RuntimeError('Timeout context manager should be used '
RuntimeError: Timeout context manager should be used inside a task

Is there any way I can fix this? The thing I want to do is to get update from telegram api -> process it asynchronious with aiogram’s bot -> send user a message.

Is there any way I can deal with it or should I use starlette and telegram bot separately (that is not prefered, as I have some other endpoints to same telegram user/chat data into my mongoDB).

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
evgfilim1commented, Jun 23, 2020

Yeah, that’s what I thought about. You’re using uvloop (uvicorn’s dependency). It’s known (but not yet properly reported) bug with aiohttp, uvloop and Python 3.7+

ref: MagicStack/uvloop#274

0reactions
evgfilim1commented, Jun 26, 2020

As far as I know, aiohttp works perfectly with uvloop

@uwinx it does work, but not with timers (at least in aiogram, IDK why) By saying “it doesn’t work” I mean timers (see traceback, @vitaliy-diachkov is not the only one who faces this, at least me too)

let us know here if we can close it?

Don’t close it as it is still a bug in aiogram

Read more comments on GitHub >

github_iconTop Results From Across the Web

Timeout context manager should be used inside a task - Stack ...
aiohttp.ClientSession() wants to be called inside a coroutine. Try moving your Client() initializer into any async def function.
Read more >
Timeout context manager should be used inside a task · Issue ...
Just today I started having this issue when trying (so far just with SQS) Context i'm calling from inside an aiohttp server request...
Read more >
error with request Timeout context manager should be used ...
i cant work out where i am going worng i am in process in moving from an sync to async.
Read more >
aio-libs/Lobby - Gitter
Timeout context manager should be used inside a task. I'm accessing aiohttp inside a handler coroutine executed inside asyncio.start_server(). Joongi Kim.
Read more >
Timeout context manager should be used inside a task` in ...
[Solved] `RuntimeError: Timeout context manager should be used inside a task` in flask api application. Guest. # testing_scraper_functions.py scraper = Scraper( ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found