RuntimeError: Cannot enter into task

See original GitHub issue

Hi,

thanks for this amazing package, this was for me the reason to adopt asyncio (otherwise the maintenance burden was too high).

I sometimes see this error in CI systems, and have trouble reproducing it locally:

Traceback (most recent call last):
  File "/Users/maartenbreddels/miniconda3/envs/dev/lib/python3.7/site-packages/nest_asyncio.py", line 149, in run
    ctx.run(self._callback, *self._args)
RuntimeError: Cannot enter into task <Task pending coro=<_debounced_callable.__call__.<locals>.debounced_execute.<locals>.run_async() running at /Users/maartenbreddels/src/vaex/packages/vaex-core/vaex/jupyter/utils.py:149>> while another task <Task pending coro=<InteractiveShell.run_cell_async() running at /Users/maartenbreddels/miniconda3/envs/dev/lib/python3.7/site-packages/IPython/core/interactiveshell.py:3020> cb=[IPythonKernel._cancel_on_sigint.<locals>.cancel_unless_done(<Future pendi...ernel.py:230]>)() at /Users/maartenbreddels/miniconda3/envs/dev/lib/python3.7/site-packages/ipykernel/ipkernel.py:230, IOLoop.add_future.<locals>.<lambda>() at /Users/maartenbreddels/miniconda3/envs/dev/lib/python3.7/site-packages/tornado/ioloop.py:690]> is being executed.

I wonder if you have any idea what can cause this so I can try to reproduce it and open a proper issue (or fix it).

cheers,

Maarten

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
maartenbreddelscommented, Jul 6, 2021

For my understanding (and possibly others), this simple script will trigger this exception:

import asyncio
import nest_asyncio


async def task1():
    print("task1 before")
    await asyncio.sleep(0.4)
    print("task1 after")


async def task2():
    print("task2 before")
    await asyncio.sleep(0.4)
    print("task2 after")


async def main():
    print("main: start")
    task = asyncio.create_task(task1())
    nest_asyncio.apply()
    asyncio.run(task2())
    print("main: end")
    await task

asyncio.run(main())

Which gives the following output:

main: start
Exception in callback <TaskStepMethWrapper object at 0x102922bb0>()
handle: <Handle <TaskStepMethWrapper object at 0x102922bb0>()>
Traceback (most recent call last):
  File "/Users/maartenbreddels/miniconda3/envs/voila-dev/lib/python3.9/asyncio/events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
RuntimeError: Cannot enter into task <Task pending name='Task-2' coro=<task1() running at /Users/maartenbreddels/src/voila-dashboards/voila/cannot-enter.py:6>> while another task <Task pending name='Task-1' coro=<main() running at /Users/maartenbreddels/src/voila-dashboards/voila/cannot-enter.py:22> cb=[_run_until_complete_cb() at /Users/maartenbreddels/miniconda3/envs/voila-dev/lib/python3.9/asyncio/base_events.py:184]> is being executed.
task2 before
task2 after
main: end
# here it hangs, and after ctrl-c (twice) gives
^C^CTraceback (most recent call last):
  File "/Users/maartenbreddels/miniconda3/envs/voila-dev/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/Users/maartenbreddels/miniconda3/envs/voila-dev/lib/python3.9/asyncio/base_events.py", line 629, in run_until_complete
    self.run_forever()
  File "/Users/maartenbreddels/miniconda3/envs/voila-dev/lib/python3.9/asyncio/base_events.py", line 596, in run_forever
    self._run_once()
  File "/Users/maartenbreddels/miniconda3/envs/voila-dev/lib/python3.9/site-packages/nest_asyncio.py", line 87, in _run_once
    event_list = self._selector.select(timeout)
  File "/Users/maartenbreddels/miniconda3/envs/voila-dev/lib/python3.9/selectors.py", line 562, in select
    kev_list = self._selector.control(None, max_ev, timeout)
KeyboardInterrupt

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/maartenbreddels/src/voila-dashboards/voila/cannot-enter.py", line 26, in <module>
  File "/Users/maartenbreddels/miniconda3/envs/voila-dev/lib/python3.9/asyncio/runners.py", line 47, in run
    _cancel_all_tasks(loop)
  File "/Users/maartenbreddels/miniconda3/envs/voila-dev/lib/python3.9/asyncio/runners.py", line 63, in _cancel_all_tasks
    loop.run_until_complete(
  File "/Users/maartenbreddels/miniconda3/envs/voila-dev/lib/python3.9/site-packages/nest_asyncio.py", line 64, in run_until_complete
    self._run_once()
  File "/Users/maartenbreddels/miniconda3/envs/voila-dev/lib/python3.9/site-packages/nest_asyncio.py", line 87, in _run_once
    event_list = self._selector.select(timeout)
  File "/Users/maartenbreddels/miniconda3/envs/voila-dev/lib/python3.9/selectors.py", line 562, in select
    kev_list = self._selector.control(None, max_ev, timeout)
KeyboardInterrupt
Task was destroyed but it is pending!
task: <Task cancelling name='Task-2' coro=<task1() running at /Users/maartenbreddels/src/voila-dashboards/voila/cannot-enter.py:6> cb=[<TaskWakeupMethWrapper object at 0x102922e50>(), gather.<locals>._done_callback() at /Users/maartenbreddels/miniconda3/envs/voila-dev/lib/python3.9/asyncio/tasks.py:764]>
sys:1: RuntimeWarning: coroutine 'task1' was never awaited

Note that this confirms what @erdewit was saying, it will be triggered when asyncio machinery wants to resume a task that is unpatched (i.e. created before .apply() was called).

Hopefully, this will help to debug similar issues.

1reaction
erdewitcommented, Jul 16, 2020

RuntimeError: Cannot enter into task

This error is indicative of an unpatched Task. It could be similar to the unpatched Future from #23, were it gets imported before nest_asyncio has patched asyncio.Task.

If possible, do the import and application of nest_asyncio before importing other modules.

Read more comments on GitHub >

github_iconTop Results From Across the Web

RuntimeError: Cannot enter into task · Issue #12803 - GitHub
Description Certain notebooks raise an exception when they are opened: RuntimeError: Cannot enter into task. This is an intermittent error.
Read more >
Cannot enter into task while another task is being executed ...
I am using FastAPI, and strangely, only on the first API call I get the error "RuntimeError: Cannot enter into task while another...
Read more >
Source code for asyncio.tasks - Sphinx Book Theme
"""Support for tasks, coroutines and the scheduler. ... current_task is not None: raise RuntimeError(f"Cannot enter into task {task!r} while ...
Read more >
Common Mistakes Using Python3 asyncio
RuntimeWarning: coroutine foo was never awaited; 3. Task was destroyed but it is pending! 4. Task/Future is awaited in a different EventLoop than...
Read more >
Python Jupyter Lab で RuntimeError: Cannot enter into task <1 ...
Description Certain notebooks raise an exception when they are opened: RuntimeError: Cannot enter into task. This is an intermittent error.
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