fix(livekit): use asyncio.to_thread so status-update task actually runs#32
Open
devteamaegis wants to merge 1 commit into
Open
Conversation
…execute The LiveKit tool handler creates a _status_update task to notify the user while a tool is executing, then immediately calls tool.func() synchronously. Because tool.func() is a blocking HTTP call (using the sync AgentMail client), it never yields to the event loop — so the _status_update task is never scheduled. By the time tool.func() returns, the task is cancelled before it runs even once. The status update therefore silently never fires. Fix: off-load tool.func() to a thread via asyncio.to_thread(). This yields the event loop back to other tasks while the HTTP call runs in a worker thread, giving _status_update the opportunity to execute as intended. Adds python/tests/test_livekit_status_update.py with two test cases: - status update fires during a deliberately slow synchronous tool - tool result JSON is returned correctly after the fix
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.
Problem
The LiveKit tool handler is intended to display a status message to the user
while the underlying AgentMail API call is in flight:
tool.funccalls the synchronousAgentMailclient, which useshttpx.Clientand blocks the calling thread. In an async context that means the event
loop is blocked for the entire duration of the HTTP request. Because the
event loop cannot run other tasks while it is blocked,
_status_updateisnever scheduled. When
tool.funcfinally returns, the very next line cancelsthe task — so
generate_replyis never called and the user never sees thestatus update.
Fix
Off-load
tool.functo a thread pool withasyncio.to_thread. The eventloop stays free while the HTTP call runs in a worker thread, giving
_status_updatethe time slice it needs to run:Tests
python/tests/test_livekit_status_update.py(both pass):test_status_update_fires_during_blocking_tool— a slow sync tool is used; after releasing the gate the test verifiesgenerate_replywas calledtest_tool_result_returned_correctly— the JSON result is still returned correctly after the changeSummary by cubic
Prevents the LiveKit tool handler from blocking the event loop by running the synchronous
AgentMailHTTP call in a thread. This allows the status-update task to run so users see progress while the tool executes.tool.func(...).model_dump_json()viaasyncio.to_threadso_status_updatecan run and callgenerate_reply.Written for commit 318509e. Summary will update on new commits. Review in cubic