Skip to content

Fix simulator putfile posix error#16386

Merged
paulb777 merged 12 commits into
mainfrom
fix-simulator-putfile-posix-error
Jul 13, 2026
Merged

Fix simulator putfile posix error#16386
paulb777 merged 12 commits into
mainfrom
fix-simulator-putfile-posix-error

Conversation

@paulb777

@paulb777 paulb777 commented Jul 10, 2026

Copy link
Copy Markdown
Member

Fix Storage putFile POSIX error 40 on iOS Simulator

Description

This PR addresses issue #16351 where putFile fails on the iOS Simulator roughly 1.5–2.5 seconds after starting with Error Domain=FIRStorageErrorDomain Code=-13000 "Unexpected 40 code from backend".

The root cause is a known bug in the nsurlsessiond QUIC receive path on the iOS Simulator, which incorrectly surfaces a POSIX errno 40 (EMSGSIZE / Message too long) when transferring large datagrams via a background URL session.

Fixes implemented:

  1. Disabled Background Sessions on Simulator: Updated StorageUploadTask to explicitly disable background sessions when running on the simulator (#if targetEnvironment(simulator)). This routes the upload through a standard foreground NSURLSession in the app process, cleanly bypassing the buggy nsurlsessiond QUIC path. The behavior on real physical devices remains completely unchanged.
  2. Improved POSIX Error Formatting: Updated StorageError.swift to properly catch and format underlying NSPOSIXErrorDomain errors. Instead of the confusing and incorrect "Unexpected 40 code from backend" message, developers will now see an accurate translation, such as "POSIX errno 40 (Message too long)", preventing wasted time debugging backend configurations for local networking failures.
  3. Internal GTMSessionFetcherService caching fix: Updated StorageFetcherService so that when updateTestBlock is called during test suites, the test block is correctly applied and propagated to all pre-cached GTMSessionFetcherService buckets, ensuring reliable mock interception during tests.
  4. Integration Test Coverage: Added an integration test (StoragePOSIXErrorTest) that naturally validates the putFileAsync flow successfully uploads the payload on the simulator without throwing EMSGSIZE.

Related Issues

Fixes #16351

Testing

  • Verified FirebaseStorageIntegration test suite locally.
  • Confirmed StoragePOSIXErrorTest properly completes a file upload task on the iOS simulator environment without throwing an EMSGSIZE abort.
  • Validated error message formatting intercepts and correctly translates POSIX errno codes.

paulb777 added 2 commits July 10, 2026 16:12
Disables background session on simulator for putFile to workaround a QUIC EMSGSIZE error, formats POSIX errors properly, and adds an integration test.
@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.

@paulb777

Copy link
Copy Markdown
Member Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request addresses an issue where putFile fails on the iOS Simulator with POSIX errno 40 (EMSGSIZE) by disabling background session uploads in simulator environments. It also improves error formatting for NSPOSIXErrorDomain errors and ensures testBlock is correctly propagated across all fetcher services. Feedback on these changes includes a recommendation to use Int32(truncatingIfNeeded:) to prevent potential crashes when formatting POSIX errors, a suggestion to simplify the conditional compilation block for simulator checks, and a best-practice recommendation to clean up temporary files created during integration testing.

Comment thread FirebaseStorage/Sources/StorageError.swift Outdated
Comment thread FirebaseStorage/Sources/StorageUploadTask.swift
Comment thread FirebaseStorage/Tests/Integration/StoragePOSIXErrorTest.swift
@paulb777

Copy link
Copy Markdown
Member Author

/gemini review

@paulb777 paulb777 closed this Jul 10, 2026
@paulb777 paulb777 reopened this Jul 10, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request addresses an issue where putFile fails on the iOS Simulator with POSIX errno 40 (EMSGSIZE) by disabling background session uploads on simulator targets. It also improves error formatting for NSPOSIXErrorDomain errors and ensures testBlock is propagated to all fetcher services. The feedback suggests simplifying the new integration test by removing the redundant do-catch block and adding a dedicated unit test to verify the POSIX error formatting logic.

Comment thread FirebaseStorage/Tests/Integration/StoragePOSIXErrorTest.swift Outdated
Comment thread FirebaseStorage/Tests/Integration/StoragePOSIXErrorTest.swift
@paulb777

Copy link
Copy Markdown
Member Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request addresses an issue where putFile fails on the iOS Simulator with POSIX errno 40 (EMSGSIZE) by disabling background session uploads on simulator targets, propagating test blocks to all fetcher services, and improving error formatting for NSPOSIXErrorDomain errors. The review feedback suggests replacing the thread-unsafe strerror function with strerror_r to avoid potential race conditions, and utilizing defer blocks in the unit tests to guarantee proper cleanup of global test blocks and temporary files in case of test failures.

Comment thread FirebaseStorage/Sources/StorageError.swift Outdated
Comment thread FirebaseStorage/Tests/Unit/StoragePutFileTests.swift Outdated
@paulb777

Copy link
Copy Markdown
Member Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request addresses an issue where putFile could fail on the iOS Simulator with POSIX errno 40 (EMSGSIZE) due to a QUIC bug in background sessions. It disables background sessions on the simulator, improves error formatting for NSPOSIXErrorDomain errors using strerror_r, and ensures test blocks are correctly propagated to all active fetcher services. Feedback on these changes includes a recommendation to wrap the multi-statement default branch in StorageError.swift in an immediately-invoked closure to prevent potential compilation failures in Swift 5.9+ switch expressions, as well as a suggestion to simplify nested dictionary iteration in StorageFetcherService.swift by using .values.

Comment thread FirebaseStorage/Sources/StorageError.swift Outdated
Comment thread FirebaseStorage/Sources/Internal/StorageFetcherService.swift Outdated
@paulb777

Copy link
Copy Markdown
Member Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request resolves an issue where putFile fails on the iOS Simulator with POSIX errno 40 (EMSGSIZE) due to a QUIC bug in background sessions. It disables background sessions on the simulator, improves error formatting for NSPOSIXErrorDomain errors, updates test block propagation in StorageFetcherService, and adds corresponding tests. The review feedback suggests simplifying the conditional compilation block in StorageUploadTask.swift to avoid duplicating assignments, and refactoring the immediately-invoked closure in StorageError.swift using a ternary operator and strerror for better readability.

Comment thread FirebaseStorage/Sources/StorageUploadTask.swift
Comment thread FirebaseStorage/Sources/StorageError.swift Outdated
@paulb777 paulb777 requested a review from ncooke3 July 11, 2026 02:19
@paulb777 paulb777 added this to the 12.17.0 - M184 milestone Jul 11, 2026
Comment thread FirebaseStorage/Sources/StorageError.swift Outdated
Comment thread FirebaseStorage/Tests/Integration/StoragePOSIXErrorTest.swift Outdated

@ncooke3 ncooke3 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@paulb777 paulb777 merged commit 9be316b into main Jul 13, 2026
67 checks passed
@paulb777 paulb777 deleted the fix-simulator-putfile-posix-error branch July 13, 2026 20:43
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.

Storage: putFile fails on iOS Simulator with "Unexpected 40 code from backend" — errno 40 (EMSGSIZE) from QUIC recv path, not an HTTP status

2 participants