Closed vanessa-bonifaz closed 3 weeks ago
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.
Seems related:
https://github.com/gradio-app/gradio/issues/5941
Another workaround is to do pip uninstall uvloop
.
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 https://github.com/encode/uvicorn/discussions/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)
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)'."
The code works alright in Google Colab notebook:
How can I solve this issue?
Have you searched existing issues? π
Reproduction
Screenshot
No response
Logs
No response
System Info
Severity
Blocking usage of gradio