fix: keep a strong reference to the websocket stream task in autogen-studio#7825
Open
kratos0718 wants to merge 1 commit into
Open
fix: keep a strong reference to the websocket stream task in autogen-studio#7825kratos0718 wants to merge 1 commit into
kratos0718 wants to merge 1 commit into
Conversation
The run websocket handler starts ws_manager.start_stream() with a bare asyncio.create_task() and discards the returned task. The event loop only keeps a weak reference to a task, so it can be garbage-collected before start_stream finishes — silently dropping a user's run mid-stream. Track the task in a module-level set and remove it on completion via add_done_callback so a strong reference is held until it finishes.
avinashkamat48-design
left a comment
There was a problem hiding this comment.
Keeping a strong reference fixes the GC risk, but this still drops task failures silently. If ws_manager.start_stream(...) raises, the done callback only discards the task from _background_tasks; nobody retrieves task.exception(), logs it, or notifies the websocket/client. Could the callback inspect/log failures so background stream errors do not become invisible?
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.
Why are these changes needed?
The run websocket handler in
autogen-studio(web/routes/ws.py) starts the stream with a bareasyncio.create_task(...)and discards the returned task:Per the asyncio docs, the event loop only keeps a weak reference to a task. A task with no other reference can be garbage-collected before it finishes — here that means a user's streaming run can be silently dropped mid-flight under GC pressure, with no error surfaced.
Fix
Keep a strong reference to the task in a module-level set and remove it on completion via
add_done_callback(the pattern recommended in the asyncio docs):No behavior change beyond ensuring the stream task is retained until it completes. Single file.
Related issue number
N/A — small self-contained reliability fix.
Checks
python -m py_compile).