Skip to content

fix(ailogic): stream errors should not be ignored#16298

Draft
ncooke3 wants to merge 10 commits into
mainfrom
nc.xcode27.ailogic.2
Draft

fix(ailogic): stream errors should not be ignored#16298
ncooke3 wants to merge 10 commits into
mainfrom
nc.xcode27.ailogic.2

Conversation

@ncooke3

@ncooke3 ncooke3 commented Jun 18, 2026

Copy link
Copy Markdown
Member

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.
Screenshot 2026-06-18 at 5 33 51 PM

1. Swallowed Mid-Stream Errors (Stall)

  • Issue: Network errors during URLSession.AsyncBytes iteration threw silently because the unstructured Task in loadRequestStream lacked a do-catch block. This caused the continuation to stall infinitely.
  • Fix: Wrapped the Task logic in a top-level do-catch that properly forwards errors via continuation.finish(throwing: error).

2. Missing Cancellation (Resource Leak)

  • Issue: If a consumer broke out of the stream early, the AsyncThrowingStream continuation terminated, but the internal network request silently continued downloading and parsing in the background.
  • Fix: Added an onTermination handler to the continuation (continuation.onTermination = { task.cancel() }) in both GenerativeAIService.swift and GenerativeModel.swift to explicitly cancel the underlying Task and abort the network connection.

Testing

  • testGenerateContentStream_failure_midStreamError_throwsError: Verifies mid-stream drops throw correctly.
  • testGenerateContentStream_cancellation_resourceLeak: Verifies early exit correctly invokes URLProtocol.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

      - NOTE  | [OSX] xcodebuild:  /Users/runner/work/firebase-ios-sdk/firebase-ios-sdk/FirebaseAI/Tests/Unit/GenerativeAIServiceTests.swift:253: error: -[FirebaseAILogic_Unit_unit.GenerativeAIServiceTests testGenerateContentStream_cancellation_resourceLeak] : Asynchronous wait failed: Exceeded timeout of 2 seconds, with unfulfilled expectations: "stopLoading should be called when task is cancelled".
      - NOTE  | [OSX] xcodebuild:  2026-06-18 21:45:41.407428+0000 xctest[12573:40480] [[FirebaseAI]] 12.16.0 - [FirebaseAI][I-VTX002003] The server responded with an error: <NSHTTPURLResponse: 0x600002cdd7a0> { URL: https://firebasevertexai.googleapis.com/v1beta/projects/test-project-id/locations/test-location/publishers/google/models/test-model:streamGenerateContent?alt=sse } { Status Code: 400, Headers {
      - NOTE  | [OSX] xcodebuild:  /Users/runner/work/firebase-ios-sdk/firebase-ios-sdk/FirebaseAI/Tests/Unit/GenerativeAIServiceTests.swift:197: error: -[FirebaseAILogic_Unit_unit.GenerativeAIServiceTests testGenerateContentStream_failure_midStreamError_badResponse_throwsError] : Asynchronous wait failed: Exceeded timeout of 4 seconds, with unfulfilled expectations: "Stream should throw URLError(.networkConnectionLost)".
      - NOTE  | [OSX] xcodebuild:  /Users/runner/work/firebase-ios-sdk/firebase-ios-sdk/FirebaseAI/Tests/Unit/GenerativeAIServiceTests.swift:142: error: -[FirebaseAILogic_Unit_unit.GenerativeAIServiceTests testGenerateContentStream_failure_midStreamError_throwsError] : Asynchronous wait failed: Exceeded timeout of 4 seconds, with unfulfilled expectations: "Stream should throw URLError(.networkConnectionLost)".

@gemini-code-assist

Copy link
Copy Markdown
Contributor
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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.

Comment on lines +75 to 76
let task = Task {
do {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The entire contents of the task is in wrapped in a do/catch.

continuation.finish(throwing: nil)
} catch {
continuation.finish(throwing: error)
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This catch block catches the errors from the above task that were previously caught on a per-throwing statement basis.

Comment on lines +137 to +138
continuation.onTermination = { @Sendable _ in
task.cancel()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is new but recommended and required for the new testGenerateContentStream_cancellation_resourceLeak to pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant