Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeError: There is no current event loop in thread 'Thread-1 (thread_inner)'. #1504

Open
alijaafar21 opened this issue Mar 10, 2024 · 5 comments

Comments

@alijaafar21
Copy link

alijaafar21 commented Mar 10, 2024

Hello,

While attempting to invoke a deployed Modal function in a Chainlit application I faced the following error:

RuntimeError: There is no current event loop in thread 'Thread-1 (thread_inner)'.

This is how I am getting and invoking the function:

query_mistral_7b_instruct_v0p2 = modal.Function.from_name(app_name='...', tag='query_mistral_7b_instruct_v0p2')
response = query_mistral_7b_instruct_v0p2.remote('What is the meaning of a word?')

Kindly note that the same error was first happening when getting the function through modal.Function.lookup( )
but using modal.Function.from_name( ) made it work.
But now the .remote( ) is causing the same issue.

Here's the full stack trace:

Exception in thread Thread-1 (thread_inner):
Traceback (most recent call last):
  File "...\Python\Python312\Lib\threading.py", line 1073, in _bootstrap_inner
    self.run()
  File "...\Python\Python312\Lib\threading.py", line 1010, in run
    self._target(*self._args, **self._kwargs)
  File "...\venv\Lib\site-packages\synchronicity\synchronizer.py", line 168, in thread_inner        
    raise exc
  File "...\venv\Lib\site-packages\synchronicity\synchronizer.py", line 162, in thread_inner        
    asyncio.run(loop_inner())
  File "...\venv\Lib\site-packages\nest_asyncio.py", line 26, in run
    loop = asyncio.get_event_loop()
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\venv\Lib\site-packages\nest_asyncio.py", line 40, in _get_event_loop
    loop = events.get_event_loop_policy().get_event_loop()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\Python\Python312\Lib\asyncio\events.py", line 702, 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-1 (thread_inner)'.
  • Python version: 3.12.2
  • Modal Client version: 0.61.31
@alijaafar21
Copy link
Author

alijaafar21 commented Mar 11, 2024

Update:

After some searching, I came across the following PR that fixes a similar issue RuntimeError on nest_asyncio.apply() (There is no current event loop in thread 'Thread-3' ) by adding the following:

import asyncio, nest_asyncio

try:
    asyncio.get_event_loop()
    nest_asyncio.apply()
except RuntimeError:
    pass

Indeed, putting this code snippet in the same file that contains the invocation of the Modal function fixed the problem!

Thank you @eugeniashurko !

@mwaskom
Copy link
Contributor

mwaskom commented Mar 11, 2024

Hey @alijaafar21 just so I understand — is nest_asyncio a library you're using in your codebase? Any chance you have a minimal reproducer we could run to understand the underlying issue?

@alijaafar21
Copy link
Author

Hello,

I am not directly using nest_asyncio in my code. But, I am working on a Chainlit app, which perhaps uses it.

I have created the following repo which will allow reproducing the error reported above by executing

chainlit run app.py

In app.py, I am trying to lookup a function and then invoke it.

Kindly note that if the try-except block in app.py is uncommented, the error no longer happens.

@mwaskom
Copy link
Contributor

mwaskom commented Mar 11, 2024

Thank you! I suspect that the issue is with the code underlying the philosophy-question-answerer app though and it's going to be hard to debug that without knowing what it looks like. Definitely would be easier for us to resolve this if we had a simple example we could run on our end to observe the error, although I appreciate that that may be difficult to produce.

@alijaafar21
Copy link
Author

alijaafar21 commented Mar 11, 2024

Kindly find the code of the function query_mistral_7b_instruct_v0p2 we are attempting to invoke here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants