fix(py): Handle stream task cancellation without crashing callback#4953
fix(py): Handle stream task cancellation without crashing callback#4953huangjeff5 wants to merge 2 commits intomainfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request primarily focuses on enhancing the robustness of asynchronous stream handling within the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
|
|
||
| @pytest.mark.asyncio | ||
| async def test_stream_cancellation_does_not_crash_callback() -> None: | ||
| """ACC-562: When stream task is cancelled, the channel.closed callback must not crash. |
There was a problem hiding this comment.
Fix comment format
There was a problem hiding this comment.
Code Review
This pull request addresses a potential crash when a streaming action is cancelled. The fix correctly propagates the cancellation status from the underlying task to the result future, preventing hangs or unhandled exceptions. A new asynchronous test case has been added to verify this fix by simulating a stream cancellation and asserting the correct CancelledError is raised. The changes appear correct and improve the robustness of streaming actions.
It's worth noting that the pull request title and description refer to a JSON serialization issue in FastAPI/Flask, which does not seem to match the content of the changes. It would be beneficial to update them to accurately reflect the work done on stream cancellation handling.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
When the stream task is cancelled (e.g., client disconnects during streaming), the channel.closed done callback previously called result() on a cancelled future, which raised CancelledError and crashed the callback.
Fix: Add _propagate_closed_to_result that checks cancelled/exception/result before acting. When channel.closed is cancelled, cancel result_future instead of calling result(). Propagates cancellation cleanly to consumers.