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

I’m not sure if this is a discord.py problem or an asyncio problem or just an operator error, so if it isn’t discord.py’s problem - my apologies.

dl = asyncio.new_event_loop()

dl.run_until_complete(login())

produces

Traceback (most recent call last):
  File "./daemon.py", line 98, in <module>
    dl.run_until_complete(login())
  File "/usr/lib/python3.5/asyncio/base_events.py", line 387, in run_until_complete
    return future.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
    raise self._exception
  File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
    result = coro.send(None)
  File "./daemon.py", line 92, in login
    await client.login(config["token"])
  File "/home/zenforic/.local/lib/python3.5/site-packages/discord/client.py", line 416, in login
    yield from getattr(self, '_login_' + str(n))(*args, **kwargs)
  File "/home/zenforic/.local/lib/python3.5/site-packages/discord/client.py", line 346, in _login_1
    data = yield from self.http.static_login(token, bot=is_bot)
  File "/home/zenforic/.local/lib/python3.5/site-packages/discord/http.py", line 258, in static_login
    data = yield from self.request(Route('GET', '/users/@me'))
  File "/home/zenforic/.local/lib/python3.5/site-packages/discord/http.py", line 137, in request
    r = yield from self.session.request(method, url, **kwargs)
  File "/home/zenforic/.local/lib/python3.5/site-packages/aiohttp/client.py", line 555, in __iter__
    resp = yield from self._coro
  File "/home/zenforic/.local/lib/python3.5/site-packages/aiohttp/client.py", line 197, in _request
    with Timeout(timeout, loop=self._loop):
  File "/home/zenforic/.local/lib/python3.5/site-packages/async_timeout/__init__.py", line 36, in __enter__
    raise RuntimeError('Timeout context manager should be used '
RuntimeError: Timeout context manager should be used inside a task
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7fd967aa0b00>

however, it does not give the error if I use

loop = asyncio.get_event_loop()

loop.run_until_complete(login())

but the problem with that is it blocks the rest of the file, which won’t work.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
Heanthorcommented, Jun 8, 2018

@Gorialis I was having this same issue and your comment helped me fix it. For anyone else having this problem, here’s what I did:

def start_loop(loop, gen):
    loop.run_until_complete(gen)


class DiscordAccessor:
    discord_loop = asyncio.new_event_loop()
    client = discord.Client(loop=discord_loop)

    def __init__(self):
        self.channel_id = 'channelID'

        self.discord_coroutine = DiscordAccessor.client.start('token')

        self.discord_thread = Thread(target=start_loop, args=(self.discord_loop, self.discord_coroutine))
        self.discord_thread.start()

The event loop is set when initializing the client, and the separate thread blocks on the same loop. Otherwise, the client attempts to run on the event loop of the thread used to create it (client.py:142).

5reactions
khazhykcommented, Aug 10, 2017

just use bot.run()

Look at the examples directory, it has plenty of working bots

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