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]: Asyncio task created without holding a reference to it in astream_chat() #17801

Closed
andrewisplinghoff opened this issue Feb 12, 2025 · 2 comments · Fixed by #17812
Closed
Labels
bug Something isn't working triage Issue needs to be triaged/prioritized

Comments

@andrewisplinghoff
Copy link
Contributor

andrewisplinghoff commented Feb 12, 2025

Bug Description

In the following two astream_chat() implementations, asyncio tasks are being created that are not awaited and also not being referenced after being created:

asyncio.create_task(chat_response.awrite_response_to_history(self._memory))

asyncio.create_task(response.awrite_response_to_history(self._memory))

To my current understanding, this leads to undefined behaviour as the Python Garbage Collector is free to remove these unreachable objects at any point in time:
https://textual.textualize.io/blog/2023/02/11/the-heisenbug-lurking-in-your-async-code/

Version

v0.12.16

Relevant Logs/Tracbacks

Some background info:
Started looking at this when I noticed messages such as the following when overloading the LLM behind the application, leading to connection errors:

ERROR:    asyncio [12-02-2025 14:07:13] Task exception was never retrieved
future: <Task finished name='Task-794' coro=<Dispatcher.span.<locals>.async_wrapper() done, defined at /usr/local/lib/python3.12/site-packages/llama_index/core/instrumentation/dispatcher.py:349> exception=APIConnectionError('Connection error.')>

This is also a sign that there was a task that never was awaited (not sure if this happened when running into the exception during the exact task linked above, though).

@andrewisplinghoff andrewisplinghoff added bug Something isn't working triage Issue needs to be triaged/prioritized labels Feb 12, 2025
@run-llama run-llama deleted a comment from dosubot bot Feb 12, 2025
@logan-markewich
Copy link
Collaborator

imo I would really move to using AgentWorkflow for single agents [1] and multi agents [2] -- the implementation in these older agent classes is very janky due to needing to expose a generator return type

That being said, probably the fix here is to store the reference to the task on the StreamingAgentChatResponse object, and handle/clean up it appropriately in there

@andrewisplinghoff
Copy link
Contributor Author

@logan-markewich As switching over to the workflow-based approach would be non-trivial for us, I've submitted a PR to fix this issue. Let me know what you think.

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

Successfully merging a pull request may close this issue.

2 participants