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

[Bug]: Event loop issue with OpensearchVectorClient during streaming response #12675

Closed
ttozser opened this issue Apr 9, 2024 · 6 comments
Closed
Labels
bug Something isn't working triage Issue needs to be triaged/prioritized

Comments

@ttozser
Copy link

ttozser commented Apr 9, 2024

Bug Description

I create an AsyncContentStream to return a StreamingResponse with FastAPI.
When I try to initialize OpensearchVectorClient I get the following exception

  • if the generator method is async
    |     return OpensearchVectorClient(opensearch_url, tenant, 1536,
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "...\Lib\site-packages\llama_index\vector_stores\opensearch\base.py", line 324, in __init__
    |     event_loop.run_until_complete(
    |   File "...\Lib\asyncio\base_events.py", line 629, in run_until_complete
    |     self._check_running()
    |   File "...\Lib\asyncio\base_events.py", line 588, in _check_running
    |     raise RuntimeError('This event loop is already running')
    | RuntimeError: This event loop is already running
  • if the generator method is not async
    |     return OpensearchVectorClient(opensearch_url, tenant, 1536,
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "...\Lib\site-packages\llama_index\vector_stores\opensearch\base.py", line 322, in __init__
    |     event_loop = asyncio.get_event_loop()
    |                  ^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "...\Lib\asyncio\events.py", line 677, in get_event_loop
    |     raise RuntimeError('There is no current event loop in thread %r.'
    | RuntimeError: There is no current event loop in thread 'AnyIO worker thread'.

The same code used to work and it broke after this PR was merged #11513

Version

llama-index-core = 0.10.27, llama-index-vector-stores-opensearch = 0.1.8

Steps to Reproduce

Create an AsyncContentStream and return a StreamingResponse with FastAPI.
Try to initialize the OpensearchVectorClient inside the AsyncContentStream

Relevant Logs/Tracbacks

|     return OpensearchVectorClient(opensearch_url, tenant, 1536,
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "...\Lib\site-packages\llama_index\vector_stores\opensearch\base.py", line 324, in __init__
    |     event_loop.run_until_complete(
    |   File "...\Lib\asyncio\base_events.py", line 629, in run_until_complete
    |     self._check_running()
    |   File "...\Lib\asyncio\base_events.py", line 588, in _check_running
    |     raise RuntimeError('This event loop is already running')
    | RuntimeError: This event loop is already running

-----------------------

    |     return OpensearchVectorClient(opensearch_url, tenant, 1536,
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "...\Lib\site-packages\llama_index\vector_stores\opensearch\base.py", line 322, in __init__
    |     event_loop = asyncio.get_event_loop()
    |                  ^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "...\Lib\asyncio\events.py", line 677, in get_event_loop
    |     raise RuntimeError('There is no current event loop in thread %r.'
    | RuntimeError: There is no current event loop in thread 'AnyIO worker thread'.
@ttozser ttozser added bug Something isn't working triage Issue needs to be triaged/prioritized labels Apr 9, 2024
Copy link

dosubot bot commented Apr 9, 2024

Hey @ttozser, good to see you around here again! 🚀 I'm diving into your issue right now and will circle back with insights shortly. Hang tight! 🛸

@ttozser
Copy link
Author

ttozser commented Apr 9, 2024

@ahs8w maybe you can help me with this issue
thank you in advance

Copy link

dosubot bot commented Apr 9, 2024

@ahs8w, it looks like @ttozser is seeking your expertise on an issue with OpensearchVectorClient during streaming response in FastAPI. Could you please assist? Thank you in advance!

About Dosu

This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

@logan-markewich
Copy link
Collaborator

logan-markewich commented Apr 9, 2024

@ttozser use asyncio loop type, enable nesting.

import nest_asyncio 
nest_asyncio.apply()

...
uvicorn.run(..., loop="asyncio")

@ttozser
Copy link
Author

ttozser commented Apr 9, 2024

Thank you.
This fixes the problem.

@ttozser ttozser closed this as completed Apr 9, 2024
@zqf03118
Copy link

zqf03118 commented Jul 4, 2024

i use this, can only use one request,second request happen error, RuntimeError: Timeout context manager should be used inside a task,why?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage Issue needs to be triaged/prioritized
Projects
None yet
Development

No branches or pull requests

3 participants