Colab Enterprise and Gradio Error: "There is no current event loop in thread"
Closed this issue ยท 11 comments
Describe the bug
When running this code on a GCP Colab Enterprise Python Notebook, I'm getting error "RuntimeError: There is no current event loop in thread 'Thread-606 (run)'."
#! pip install --upgrade gradio
import time
import gradio as gr
def slow_echo(message, history):
for i in range(len(message)):
time.sleep(0.3)
yield "You typed: " + message[: i+1]
gr.ChatInterface(slow_echo).launch()
The code works alright in Google Colab notebook:
How can I solve this issue?
Have you searched existing issues? ๐
- I have searched and found no existing issues
Reproduction
#! pip install --upgrade gradio
import time
import gradio as gr
def slow_echo(message, history):
for i in range(len(message)):
time.sleep(0.3)
yield "You typed: " + message[: i+1]
gr.ChatInterface(slow_echo).launch()
Screenshot
No response
Logs
No response
System Info
Gradio Environment Information:
------------------------------
Operating System: Linux
gradio version: 4.29.0
gradio_client version: 0.16.1
------------------------------------------------
gradio dependencies in your environment:
aiofiles: 23.2.1
altair: 4.2.2
fastapi: 0.111.0
ffmpy: 0.3.2
gradio-client==0.16.1 is not installed.
httpx: 0.27.0
huggingface-hub: 0.20.3
importlib-resources: 6.4.0
jinja2: 3.1.3
markupsafe: 2.1.5
matplotlib: 3.7.1
numpy: 1.25.2
orjson: 3.10.3
packaging: 24.0
pandas: 2.0.3
pillow: 9.4.0
pydantic: 2.7.1
pydub: 0.25.1
python-multipart: 0.0.9
pyyaml: 6.0.1
ruff: 0.4.3
semantic-version: 2.10.0
tomlkit==0.12.0 is not installed.
typer: 0.12.3
typing-extensions: 4.11.0
urllib3: 2.0.7
uvicorn: 0.29.0
authlib; extra == 'oauth' is not installed.
itsdangerous; extra == 'oauth' is not installed.
gradio_client dependencies in your environment:
fsspec: 2023.6.0
httpx: 0.27.0
huggingface-hub: 0.20.3
packaging: 24.0
typing-extensions: 4.11.0
websockets: 11.0.3
Severity
Blocking usage of gradio
Hi @vanessa-bonifaz ! It seems to be an issue with the Colab Enterprise, not Gradio, since Gradio works fine in normal Colab. I would contact their support team.
Facing the same issue in Colab Enterprise, works fine in free Colab.
import gradio as gr
def greet(name):
return "Hello " + name + "!"
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
demo.launch(share=True, debug=True)
Exception in thread Thread-13 (run):
Traceback (most recent call last):
File "/usr/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
self.run()
File "/usr/lib/python3.10/threading.py", line 953, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.10/dist-packages/uvicorn/server.py", line 65, in run
return asyncio.run(self.serve(sockets=sockets))
File "/usr/local/lib/python3.10/dist-packages/nest_asyncio.py", line 26, in run
loop = asyncio.get_event_loop()
File "/usr/local/lib/python3.10/dist-packages/nest_asyncio.py", line 40, in _get_event_loop
loop = events.get_event_loop_policy().get_event_loop()
File "/usr/lib/python3.10/asyncio/events.py", line 656, in get_event_loop
raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-13 (run)'.
Starting...
127.0.0.1
/usr/lib/python3.10/threading.py:1018: RuntimeWarning: coroutine 'Server.serve' was never awaited
self._invoke_excepthook(self)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Exception in thread Thread-14 (run):
Traceback (most recent call last):
File "/usr/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
self.run()
File "/usr/lib/python3.10/threading.py", line 953, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.10/dist-packages/uvicorn/server.py", line 65, in run
return asyncio.run(self.serve(sockets=sockets))
File "/usr/local/lib/python3.10/dist-packages/nest_asyncio.py", line 26, in run
loop = asyncio.get_event_loop()
File "/usr/local/lib/python3.10/dist-packages/nest_asyncio.py", line 40, in _get_event_loop
loop = events.get_event_loop_policy().get_event_loop()
File "/usr/lib/python3.10/asyncio/events.py", line 656, in get_event_loop
raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-14 (run)'.
Starting...
127.0.0.1
Exception in thread Thread-15 (run):
Traceback (most recent call last):
File "/usr/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
self.run()
File "/usr/lib/python3.10/threading.py", line 953, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.10/dist-packages/uvicorn/server.py", line 65, in run
return asyncio.run(self.serve(sockets=sockets))
File "/usr/local/lib/python3.10/dist-packages/nest_asyncio.py", line 26, in run
loop = asyncio.get_event_loop()
File "/usr/local/lib/python3.10/dist-packages/nest_asyncio.py", line 40, in _get_event_loop
loop = events.get_event_loop_policy().get_event_loop()
File "/usr/lib/python3.10/asyncio/events.py", line 656, in get_event_loop
raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-15 (run)'.
Starting...
127.0.0.1
Exception in thread Thread-16 (run):
Traceback (most recent call last):
File "/usr/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
self.run()
File "/usr/lib/python3.10/threading.py", line 953, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.10/dist-packages/uvicorn/server.py", line 65, in run
return asyncio.run(self.serve(sockets=sockets))
File "/usr/local/lib/python3.10/dist-packages/nest_asyncio.py", line 26, in run
loop = asyncio.get_event_loop()
File "/usr/local/lib/python3.10/dist-packages/nest_asyncio.py", line 40, in _get_event_loop
loop = events.get_event_loop_policy().get_event_loop()
File "/usr/lib/python3.10/asyncio/events.py", line 656, in get_event_loop
raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-16 (run)'.
Starting...
127.0.0.1
Exception in thread Thread-17 (run):
Traceback (most recent call last):
File "/usr/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
self.run()
File "/usr/lib/python3.10/threading.py", line 953, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.10/dist-packages/uvicorn/server.py", line 65, in run
return asyncio.run(self.serve(sockets=sockets))
File "/usr/local/lib/python3.10/dist-packages/nest_asyncio.py", line 26, in run
loop = asyncio.get_event_loop()
File "/usr/local/lib/python3.10/dist-packages/nest_asyncio.py", line 40, in _get_event_loop
loop = events.get_event_loop_policy().get_event_loop()
File "/usr/lib/python3.10/asyncio/events.py", line 656, in get_event_loop
raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-17 (run)'.
Starting...
127.0.0.1
Exception in thread Thread-18 (run):
Traceback (most recent call last):
File "/usr/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
self.run()
File "/usr/lib/python3.10/threading.py", line 953, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.10/dist-packages/uvicorn/server.py", line 65, in run
return asyncio.run(self.serve(sockets=sockets))
File "/usr/local/lib/python3.10/dist-packages/nest_asyncio.py", line 26, in run
loop = asyncio.get_event_loop()
File "/usr/local/lib/python3.10/dist-packages/nest_asyncio.py", line 40, in _get_event_loop
loop = events.get_event_loop_policy().get_event_loop()
File "/usr/lib/python3.10/asyncio/events.py", line 656, in get_event_loop
raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-18 (run)'.
Starting...
127.0.0.1
---------------------------------------------------------------------------
KeyboardInterrupt Traceback (most recent call last)
[<ipython-input-1-726b27cae8d1>](https://localhost:8080/#) in <cell line: 12>()
10
11 demo = gr.Interface(fn=greet, inputs="text", outputs="text")
---> 12 demo.launch(share=False, inbrowser=True, debug=True)
2 frames
[/usr/local/lib/python3.10/dist-packages/gradio/blocks.py](https://localhost:8080/#) in launch(self, inline, inbrowser, share, debug, max_threads, auth, auth_message, prevent_thread_lock, show_error, server_name, server_port, height, width, favicon_path, ssl_keyfile, ssl_certfile, ssl_keyfile_password, ssl_verify, quiet, show_api, allowed_paths, blocked_paths, root_path, app_kwargs, state_session_capacity, share_server_address, share_server_protocol, auth_dependency, max_file_size, _frontend)
2215 local_url,
2216 server,
-> 2217 ) = http_server.start_server(
2218 app=self.app,
2219 server_name=server_name,
[/usr/local/lib/python3.10/dist-packages/gradio/http_server.py](https://localhost:8080/#) in start_server(app, server_name, server_port, ssl_keyfile, ssl_certfile, ssl_keyfile_password)
149 )
150 server = Server(config=config, reloader=reloader)
--> 151 server.run_in_thread()
152 break
153 except (OSError, ServerFailedToStartError):
[/usr/local/lib/python3.10/dist-packages/gradio/http_server.py](https://localhost:8080/#) in run_in_thread(self)
57 start = time.time()
58 while not self.started:
---> 59 time.sleep(1e-3)
60 if time.time() - start > 5:
61 raise ServerFailedToStartError(
@freddyaboulton When I try to run the same code in Colab Enterprise Terminal, it works fine. Fails when running the same code inside Notebook.
/content# python
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gradio as gr
>>>
>>> def greet(name):
... return "Hello " + name + "!"
...
>>> demo = gr.Interface(fn=greet, inputs="text", outputs="text")
>>> demo.launch(share=True)
Running on local URL: http://127.0.0.1:7860
Running on public URL: https://e36ae94a0ed51f0f3d.gradio.live
This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)
This is the diff for pip
: https://www.diffchecker.com/J75IJCDz/
I tried a pip freeze > requirements.txt
in Free Colab and then a pip install -r requirements.txt
in Colab Enterprise and same error.
Seems related to https://stackoverflow.com/questions/46727787/runtimeerror-there-is-no-current-event-loop-in-thread-in-async-apscheduler but looking for any insights.
Based on: https://stackoverflow.com/a/46750562/260826
Changed these lines in: /usr/local/lib/python3.10/dist-packages/nest_asyncio.py
and now works.
def _patch_asyncio():
"""Patch asyncio module to use pure Python tasks and futures."""
def run(main, *, debug=False):
# loop = asyncio.get_event_loop()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
Not sure what may be the root cause.
Thanks for the thorough deep dive @gogasca ! I don't think this is a gradio issue since nest-asyncio
is not a dependency of gradio, it's a dependency of jupyter notebook.
I am not sure what is different between enterprise and free colab based on the pip diff.
I will contact someone at google colab to let them know of the problem and I suggest you do the same so they can look into it.
I will close in the meantime! Thank you again.
The minimal repro seems to be running uvicorn in a separate thread with uvloop installed
https://colab.research.google.com/drive/1fHjPNzOJwM9KtPbhHJsNUgJEwpy5rWQP?usp=sharing
Now the previous example works fine in free Colab. Still fails in Colab Enterprise.
@freddyaboulton opened a discussion here encode/uvicorn#2342
PS: I'm part of Colab Enterprise team
Thank you @gogasca ! Subscribed to the discussion
Just to close on this thread, this will be fixed in the next Colab Enterprise image (expected to rollout next week)