aio-libs / aiohttp-devtools

dev tools for aiohttp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RuntimeWarning: coroutine 'PyCodeEventHandler.src_reload_when_live' was never awaited

Skorpyon opened this issue · comments

I've got some exception.
Can you help please understand why its happen.

[04:08:32] Restarting dev server at http://localhost:8000 ●
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/home/anton/virtualenvs/extremedialer/lib/python3.6/site-packages/watchdog/observers/api.py", line 199, in run
    self.dispatch_events(self.event_queue, self.timeout)
  File "/home/anton/virtualenvs/extremedialer/lib/python3.6/site-packages/watchdog/observers/api.py", line 368, in dispatch_events
    handler.dispatch(event)
  File "/home/anton/virtualenvs/extremedialer/lib/python3.6/site-packages/aiohttp_devtools/runserver/watch.py", line 67, in dispatch
    self.on_event(event)
  File "/home/anton/virtualenvs/extremedialer/lib/python3.6/site-packages/aiohttp_devtools/runserver/watch.py", line 88, in on_event
    self._loop.create_task(self.src_reload_when_live())
  File "/usr/lib/python3.6/asyncio/base_events.py", line 284, in create_task
    task = tasks.Task(coro, loop=self)
  File "/usr/lib/python3.6/asyncio/base_events.py", line 576, in call_soon
    self._check_thread()
  File "/usr/lib/python3.6/asyncio/base_events.py", line 615, in _check_thread
    "Non-thread-safe operation invoked on an event loop other "
RuntimeError: Non-thread-safe operation invoked on an event loop other than the current one

Task was destroyed but it is pending!
source_traceback: Object created at (most recent call last):
  File "/usr/lib/python3.6/threading.py", line 884, in _bootstrap
    self._bootstrap_inner()
  File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/home/anton/virtualenvs/extremedialer/lib/python3.6/site-packages/watchdog/observers/api.py", line 199, in run
    self.dispatch_events(self.event_queue, self.timeout)
  File "/home/anton/virtualenvs/extremedialer/lib/python3.6/site-packages/watchdog/observers/api.py", line 368, in dispatch_events
    handler.dispatch(event)
  File "/home/anton/virtualenvs/extremedialer/lib/python3.6/site-packages/aiohttp_devtools/runserver/watch.py", line 67, in dispatch
    self.on_event(event)
  File "/home/anton/virtualenvs/extremedialer/lib/python3.6/site-packages/aiohttp_devtools/runserver/watch.py", line 88, in on_event
    self._loop.create_task(self.src_reload_when_live())
  File "/usr/lib/python3.6/asyncio/base_events.py", line 284, in create_task
    task = tasks.Task(coro, loop=self)
task: <Task pending coro=<PyCodeEventHandler.src_reload_when_live() running at /home/anton/virtualenvs/extremedialer/lib/python3.6/site-packages/aiohttp_devtools/runserver/watch.py:90> created at /usr/lib/python3.6/asyncio/base_events.py:284>
/usr/lib/python3.6/threading.py:949: RuntimeWarning: coroutine 'PyCodeEventHandler.src_reload_when_live' was never awaited
  del exc_type, exc_value, exc_tb

Afraid off the top of my head I have no idea.

If you work out why this is happening or have any more details, or can show how to reproduce the error please me us know.

I started getting this error too. I know it sounds like magic, but I'm pretty sure my code didn't change since last time it worked :)
It happens whenever I save any of my .py files triggering autoreload.

looks like file watcher works in other loop compare with aiohttp

Humm, could be watchdog's thread system behaving oddly or a change in how aiohttp chooses a loop.

I think the ultimate solution would be to drop watchdog and build a new file watcher designed to work properly with asyncio, see Tinche/aiofiles#27.

I would guess that just naively iterating over files looking for changes would work on any reasonable sized project and would be suitably cross platform. Thoughts?

Actually this thing happen if PYTHONASYNCIODEBUG is enabled, because:

def _check_thread(self):
    """Check that the current thread is the thread running the event loop.
    Non-thread-safe methods of this class make this assumption and will
    likely behave incorrectly when the assumption is violated.
    Should only be called when (self._debug == True).  The caller is
    responsible for checking this condition for performance reasons.
    """
    if self._owner is None:
        return
    thread_id = threading.get_ident()
    if thread_id != self._owner:
        raise RuntimeError(
             "Non-thread-safe operation invoked on an event loop other "
             "than the current one")

in https://mail.python.org/pipermail/python-checkins/2014-December/133758.html

I started getting this error too. I know it sounds like magic, but I'm pretty sure my code didn't change since last time it worked :)
It happens whenever I save any of my .py files triggering autoreload.

Hi,

I have the same. Has anyone found a solution?

Thanks!

@achernetsky set env variable PYTHONASYNCIODEBUG=0

@Skorpyon, thank you.

But a small correction, according the documentation it is necessary to set as PYTHONASYNCIODEBUG=

That is, using aiohttp-devtools I lose all debug checks ? :(

Yes.

I'm working on what I hope to be the long term fix for this right now.

@samuelcolvin, thank you and good luck:)

I believe this should be fixed by #144, however I couldn't get the error to occur on master.

Therefore it would be wonderful if @achernetsky or @Skorpyon could try out that branch and check if the problem is resolved. Thanks in advance.