fix(ailogic): stream errors should not be ignored#16298
Conversation
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 the 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 counterproductive. 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. |
| let task = Task { | ||
| do { |
There was a problem hiding this comment.
The entire contents of the task is in wrapped in a do/catch.
| continuation.finish(throwing: nil) | ||
| } catch { | ||
| continuation.finish(throwing: error) | ||
| } |
There was a problem hiding this comment.
This catch block catches the errors from the above task that were previously caught on a per-throwing statement basis.
| continuation.onTermination = { @Sendable _ in | ||
| task.cancel() |
There was a problem hiding this comment.
This is new but recommended and required for the new testGenerateContentStream_cancellation_resourceLeak to pass.
https://github.com/firebase/firebase-ios-sdk/pull/16298/changes?w=1
Description
Fixes infinite stalls and resource leaks when streaming responses.
This surfaced as a warning.

1. Swallowed Mid-Stream Errors (Stall)
URLSession.AsyncBytesiteration threw silently because the unstructuredTaskinloadRequestStreamlacked ado-catchblock. This caused the continuation to stall infinitely.Tasklogic in a top-leveldo-catchthat properly forwards errors viacontinuation.finish(throwing: error).2. Missing Cancellation (Resource Leak)
AsyncThrowingStreamcontinuation terminated, but the internal network request silently continued downloading and parsing in the background.onTerminationhandler to the continuation (continuation.onTermination = { task.cancel() }) in bothGenerativeAIService.swiftandGenerativeModel.swiftto explicitly cancel the underlyingTaskand abort the network connection.Testing
testGenerateContentStream_failure_midStreamError_throwsError: Verifies mid-stream drops throw correctly.testGenerateContentStream_cancellation_resourceLeak: Verifies early exit correctly invokesURLProtocol.stopLoading().These failures are expected and expose the two types of bugs.
From https://github.com/firebase/firebase-ios-sdk/actions/runs/27791049770/job/82239809505?pr=16298