fix: keep strong references to PromptStreamConsumer background tasks#821
Open
kratos0718 wants to merge 1 commit into
Open
fix: keep strong references to PromptStreamConsumer background tasks#821kratos0718 wants to merge 1 commit into
kratos0718 wants to merge 1 commit into
Conversation
The consumer launched execute/improve/generate work with bare asyncio.create_task() and discarded the result. The event loop only holds a weak reference to a task, so a fire-and-forget task can be garbage-collected mid-run, silently dropping a user's prompt execution. Track spawned tasks in a set via a _spawn() helper that keeps a strong reference until completion, and cancel any pending tasks on disconnect. Closes future-agi#819
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
PromptStreamConsumerlaunched its streaming work with bareasyncio.create_task(...)and threw away the returned task in three places (execute_template_async,execute_improve_prompt_async,execute_generate_prompt_async).The event loop only keeps a weak reference to a task, so a fire-and-forget task can be garbage-collected before it finishes — silently dropping a user's prompt execution with no result and no error. Per the CPython docs: "Save a reference to the result of this function, to avoid a task disappearing mid-execution."
Closes #819.
How
self._background_tasksset and a small_spawn()helper that holds a strong reference to each task and removes it viaadd_done_callbackonce it completes (no leak).create_taskcall sites through_spawn().disconnect()so they don't outlive the connection.Notes
model_hub/utils/kb_helpers.pyuses a deprecatedasyncio.get_event_loop()plus a fire-and-forgetensure_future— happy to send a separate small PR for that to keep this one focused.Testing
python -m py_compileclean; static analyzer reports the file clean after the change.