prompt-toolkit / python-prompt-toolkit

Library for building powerful interactive command line applications in Python
https://python-prompt-toolkit.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
9.11k stars 718 forks source link

RuntimeError: no running event loop on version 3.0.37 #1726

Open kellyjonbrazil opened 1 year ago

kellyjonbrazil commented 1 year ago

My app (jellex) is getting the following traceback when starting on prompt-toolkit version 3.0.37. Previous versions of prompt-toolkit seem to work fine.

Traceback (most recent call last):
  File "/Users/kbrazil/.pyenv/versions/3.11.1/bin/jellex", line 5, in <module>
    from jellex.cli import main
  File "/Users/kbrazil/.pyenv/versions/3.11.1/lib/python3.11/site-packages/jellex/cli.py", line 231, in <module>
    query.insert_text('_')
  File "/Users/kbrazil/.pyenv/versions/3.11.1/lib/python3.11/site-packages/prompt_toolkit/buffer.py", line 1269, in insert_text
    get_app().create_background_task(self._async_completer())
  File "/Users/kbrazil/.pyenv/versions/3.11.1/lib/python3.11/site-packages/prompt_toolkit/application/application.py", line 1057, in create_background_task
    loop = self.loop or get_running_loop()
                        ^^^^^^^^^^^^^^^^^^
RuntimeError: no running event loop
sys:1: RuntimeWarning: coroutine 'Buffer._create_completer_coroutine.<locals>.async_completer' was never awaited
jonathanslenders commented 1 year ago

Hi @kellyjonbrazil,

Is it possible that query.insert_text() is called while the prompt_toolkit app is not running? Either before or after?

If it's done before starting up, could you try to call insert_text as part of a pre_run callback? This callback will be called within the event loop. https://github.com/prompt-toolkit/python-prompt-toolkit/blob/master/src/prompt_toolkit/shortcuts/prompt.py#L902 or https://github.com/prompt-toolkit/python-prompt-toolkit/blob/master/src/prompt_toolkit/application/application.py#L891

kellyjonbrazil commented 1 year ago

Thanks for the pointers - I’ll look into that.

JacobHayes commented 1 year ago

I also get this error with 3.0.37 and 3.0.38, but not 3.0.36 when using ipdb to set breakpoint()s in tests:

[...]
../../../../.pyenv/versions/3.11.0/lib/python3.11/bdb.py:90: in trace_dispatch
    return self.dispatch_line(frame)
../../../../.pyenv/versions/3.11.0/lib/python3.11/bdb.py:114: in dispatch_line
    self.user_line(frame)
../../../../.pyenv/versions/3.11.0/lib/python3.11/pdb.py:340: in user_line
    self.interaction(frame, None)
.direnv/python-3.11.0/lib/python3.11/site-packages/IPython/core/debugger.py:335: in interaction
    OldPdb.interaction(self, frame, traceback)
../../../../.pyenv/versions/3.11.0/lib/python3.11/pdb.py:435: in interaction
    self._cmdloop()
../../../../.pyenv/versions/3.11.0/lib/python3.11/pdb.py:400: in _cmdloop
    self.cmdloop()
.direnv/python-3.11.0/lib/python3.11/site-packages/IPython/terminal/debugger.py:133: in cmdloop
    ).result()
../../../../.pyenv/versions/3.11.0/lib/python3.11/concurrent/futures/_base.py:449: in result
    return self.__get_result()
../../../../.pyenv/versions/3.11.0/lib/python3.11/concurrent/futures/_base.py:401: in __get_result
    raise self._exception
../../../../.pyenv/versions/3.11.0/lib/python3.11/concurrent/futures/thread.py:58: in run
    result = self.fn(*self.args, **self.kwargs)
.direnv/python-3.11.0/lib/python3.11/site-packages/prompt_toolkit/shortcuts/prompt.py:1035: in prompt
    return self.app.run(
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <prompt_toolkit.application.application.Application object at 0x12a3b7350>, pre_run = None, set_exception_handler = True, handle_sigint = True
in_thread = False

[...]
            handle_sigint=handle_sigint,
        )
        try:
            # See whether a loop was installed already. If so, use that. That's
            # required for the input hooks to work, they are installed using
            # `set_event_loop`.
>           loop = asyncio.get_event_loop()
E           DeprecationWarning: There is no current event loop

.direnv/python-3.11.0/lib/python3.11/site-packages/prompt_toolkit/application/application.py:955: DeprecationWarning

These are the ipdb and ipython versions:

$ pip3 freeze | grep ipdb                                                                                                                                                                                                                                                                           2.65s
ipdb==0.13.13
$ pip3 freeze | grep ipython                                                                                                                                                                                                                                                                        0.34s
ipython==8.11.0

Maybe it's one of these changes in 3.0.37? https://github.com/prompt-toolkit/python-prompt-toolkit/blob/a329a88961574c0e6f6415aa46cbd69d1b4d1a22/CHANGELOG#L29-L32

Happy to open an ipdb/ipython issue if you think there's something wrong upstream.