Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Storage] Addressing Swift 6 issues with Storage's instance management #13445

Merged
merged 6 commits into from
Aug 2, 2024

Conversation

ncooke3
Copy link
Member

@ncooke3 ncooke3 commented Aug 1, 2024

For context, the original error was: Screenshot 2024-08-01 at 1 31 49 PM

The important takeaway is that if you synchronize something with an actor (e.g. instance management) in a method that isn't async like:

private class func storage(app: FirebaseApp, bucket: String) -> Storage

you'd have to change the signature of the public function which would be breaking.

So I avoided the formal route by decorating the property with nonisolated(unsafe) which tells the compiler that we know what we are doing (which we do with the locking mechanism in place). But using nonisolated(unsafe) (3f1ba97) on static properties does not work in Swift 5.5 (pitch revision). I'm not sure why it works on Swift 6.0, but the only way around this was to do it both ways for each property and differentiate with a #if swift(>=6.0) which seemed hacky.

So the next stop was to remove the properties from global state by making them instance properties. We can't do so on Storage though because there can be multiple instances of Storage (e.g. for multiple Firebase apps), so I made a new class that offers a singleton (so all Storage instances must access the same instance cache).

I couldn't make it an actor because of the first learning at the top (the actor would bubble up the added asynchronous-ness). I then ran into:
Screenshot 2024-08-01 at 5 31 15 PM
which makes sense. I added Sendable conformance, but the class doesn't meet all requirements. Particularly:

Contain only stored properties that are immutable and sendable

I tried working around this by changing the vars to lets. I tried using NSLock because it has reference semantics and we can therefore use the let:

-  private var instancesLock: os_unfair_lock = .init()
+  private let instancesLock: NSLock = .init()

But, this strategy has no corresponding solution for the var instances: [String: Storage]. I tried making it of type NSDictionary, but NSDictionary, does not conform to Sendable. We could box it the dictionary on a reference type, but you're just moving the problem down one abstraction.

So, the last option is to mark it with @unchecked Sendable which I believe means "I know what I'm doing in this type's implementation so don't worry": https://www.swift.org/migration/documentation/swift-6-concurrency-migration-guide/commonproblems/#Retroactive-Sendable-Conformance

Unfortunately, using @unchecked Sendable doesn't offer any improvements in terms of compile time safety. Since the instance cache management is used in a few places (here, GTMSessionFetcher mapping, heartbeat storage), it may make sense to carefully refactor the functionality in a generic type that lives in FirebaseSharedSwift.

private final class StorageInstanceCache: @unchecked Sendable {
  static let shared = StorageInstanceCache()

  /// A map of active instances, grouped by app. Keys are FirebaseApp names and values are
  /// instances of Storage associated with the given app.
  private var instances: [String: Storage] = [:]

  /// Lock to manage access to the instances array to avoid race conditions.
  private var instancesLock: os_unfair_lock = .init()

  /// ...

@google-oss-bot
Copy link

1 Warning
⚠️ Did you forget to add a changelog entry? (Add #no-changelog to the PR description to silence this warning.)

Generated by 🚫 Danger

Copy link
Contributor

github-actions bot commented Aug 1, 2024

✅  No API diff detected

Commit: 81dca91
Last updated: Fri Aug 2 06:26 PDT 2024
View workflow logs & download artifacts


@ncooke3 ncooke3 changed the title [Swift 6] Marking unsafe [Storage] Addressing Swift 6 issues with Storage's instance management Aug 1, 2024
@paulb777 paulb777 self-requested a review August 1, 2024 22:35
@paulb777 paulb777 self-requested a review August 2, 2024 01:53
@ncooke3 ncooke3 merged commit a3254ff into main Aug 2, 2024
47 checks passed
@ncooke3 ncooke3 deleted the nc/storage-swift6 branch August 2, 2024 13:30
cgrindel-self-hosted-renovate bot referenced this pull request in cgrindel/rules_swift_package_manager Aug 25, 2024
This PR contains the following updates:

| Package | Update | Change |
|---|---|---|
|
[firebase/firebase-ios-sdk](https://togithub.com/firebase/firebase-ios-sdk)
| major | `from: "10.29.0"` -> `from: "11.1.0"` |

---

### Release Notes

<details>
<summary>firebase/firebase-ios-sdk (firebase/firebase-ios-sdk)</summary>

###
[`v11.1.0`](https://togithub.com/firebase/firebase-ios-sdk/releases/tag/11.1.0):
Firebase Apple 11.1.0

[Compare
Source](https://togithub.com/firebase/firebase-ios-sdk/compare/11.0.0...11.1.0)

The Firebase Apple SDK (11.1.0) is now available. For more details, see
the [Firebase Apple SDK release
notes.](https://firebase.google.com/support/release-notes/ios#11.1.0)

To install this SDK, see [Add Firebase to your
project.](https://firebase.google.com/docs/ios/setup)

#### What's Changed

- \[Infra] Fetch tags first when running firebase-releaser by
[@&#8203;ncooke3](https://togithub.com/ncooke3) in
[https://github.com/firebase/firebase-ios-sdk/pull/13411](https://togithub.com/firebase/firebase-ios-sdk/pull/13411)
- Review FirebaseMessaging Tests files by
[@&#8203;MojtabaHs](https://togithub.com/MojtabaHs) in
[https://github.com/firebase/firebase-ios-sdk/pull/13402](https://togithub.com/firebase/firebase-ios-sdk/pull/13402)
- Functions Serializer Updates by
[@&#8203;yakovmanshin](https://togithub.com/yakovmanshin) in
[https://github.com/firebase/firebase-ios-sdk/pull/13409](https://togithub.com/firebase/firebase-ios-sdk/pull/13409)
- \[storage] Use async GTMSessionFetcher method by
[@&#8203;paulb777](https://togithub.com/paulb777) in
[https://github.com/firebase/firebase-ios-sdk/pull/13405](https://togithub.com/firebase/firebase-ios-sdk/pull/13405)
- \[storage] Simplify callback implementation by
[@&#8203;paulb777](https://togithub.com/paulb777) in
[https://github.com/firebase/firebase-ios-sdk/pull/13413](https://togithub.com/firebase/firebase-ios-sdk/pull/13413)
- Renamed `FUNSerializer` to `FunctionsSerializer` by
[@&#8203;yakovmanshin](https://togithub.com/yakovmanshin) in
[https://github.com/firebase/firebase-ios-sdk/pull/13410](https://togithub.com/firebase/firebase-ios-sdk/pull/13410)
- \[Infra] Upload build-from-HEAD zips on failures by
[@&#8203;ncooke3](https://togithub.com/ncooke3) in
[https://github.com/firebase/firebase-ios-sdk/pull/13414](https://togithub.com/firebase/firebase-ios-sdk/pull/13414)
- Move swiftformat options to a config file by
[@&#8203;andrewheard](https://togithub.com/andrewheard) in
[https://github.com/firebase/firebase-ios-sdk/pull/13423](https://togithub.com/firebase/firebase-ios-sdk/pull/13423)
- \[v11] Upload Carthage artifacts by
[@&#8203;ncooke3](https://togithub.com/ncooke3) in
[https://github.com/firebase/firebase-ios-sdk/pull/13425](https://togithub.com/firebase/firebase-ios-sdk/pull/13425)
- Update versions for Release 11.1.0 by
[@&#8203;ncooke3](https://togithub.com/ncooke3) in
[https://github.com/firebase/firebase-ios-sdk/pull/13426](https://togithub.com/firebase/firebase-ios-sdk/pull/13426)
- Consistent Collections Coding in `FunctionsSerializer` by
[@&#8203;yakovmanshin](https://togithub.com/yakovmanshin) in
[https://github.com/firebase/firebase-ios-sdk/pull/13419](https://togithub.com/firebase/firebase-ios-sdk/pull/13419)
- \[Auth] Add custom provider support for AuthProviderID by
[@&#8203;paulb777](https://togithub.com/paulb777) in
[https://github.com/firebase/firebase-ios-sdk/pull/13433](https://togithub.com/firebase/firebase-ios-sdk/pull/13433)
- \[Infra] Attempt to fix some Crashlytics flakes by
[@&#8203;ncooke3](https://togithub.com/ncooke3) in
[https://github.com/firebase/firebase-ios-sdk/pull/13432](https://togithub.com/firebase/firebase-ios-sdk/pull/13432)
- \[AuthErrorCode] should conform to Swift.Error by
[@&#8203;paulb777](https://togithub.com/paulb777) in
[https://github.com/firebase/firebase-ios-sdk/pull/13434](https://togithub.com/firebase/firebase-ios-sdk/pull/13434)
- \[Infra] Fix auto-tagging in `release_testing_setup.sh` for
prerelease.yml by [@&#8203;ncooke3](https://togithub.com/ncooke3) in
[https://github.com/firebase/firebase-ios-sdk/pull/13427](https://togithub.com/firebase/firebase-ios-sdk/pull/13427)
- \[AppCheck] Force link categories by
[@&#8203;ncooke3](https://togithub.com/ncooke3) in
[https://github.com/firebase/firebase-ios-sdk/pull/13435](https://togithub.com/firebase/firebase-ios-sdk/pull/13435)
- \[Infra] Quiet the git fetch
([#&#8203;13436](https://togithub.com/firebase/firebase-ios-sdk/issues/13436))
by [@&#8203;ncooke3](https://togithub.com/ncooke3) in
[https://github.com/firebase/firebase-ios-sdk/pull/13437](https://togithub.com/firebase/firebase-ios-sdk/pull/13437)
- \[Infra] Attempt to fix post-merge tagging in prerelease.yml by
[@&#8203;ncooke3](https://togithub.com/ncooke3) in
[https://github.com/firebase/firebase-ios-sdk/pull/13438](https://togithub.com/firebase/firebase-ios-sdk/pull/13438)
- \[Infra] Cleanup and small fixes for prerelease.yml by
[@&#8203;ncooke3](https://togithub.com/ncooke3) in
[https://github.com/firebase/firebase-ios-sdk/pull/13439](https://togithub.com/firebase/firebase-ios-sdk/pull/13439)
- \[Infra] Remove unneeded debug code by
[@&#8203;ncooke3](https://togithub.com/ncooke3) in
[https://github.com/firebase/firebase-ios-sdk/pull/13441](https://togithub.com/firebase/firebase-ios-sdk/pull/13441)
- \[Infra] Extend expectation wait time in FIRCLSSettingsTests.m by
[@&#8203;ncooke3](https://togithub.com/ncooke3) in
[https://github.com/firebase/firebase-ios-sdk/pull/13442](https://togithub.com/firebase/firebase-ios-sdk/pull/13442)
- \[Infra] Apply
[#&#8203;13438](https://togithub.com/firebase/firebase-ios-sdk/issues/13438)
fix to other job in 'prerelease.yml' by
[@&#8203;ncooke3](https://togithub.com/ncooke3) in
[https://github.com/firebase/firebase-ios-sdk/pull/13443](https://togithub.com/firebase/firebase-ios-sdk/pull/13443)
- Add basic EditorConfig file for repo by
[@&#8203;andrewheard](https://togithub.com/andrewheard) in
[https://github.com/firebase/firebase-ios-sdk/pull/13444](https://togithub.com/firebase/firebase-ios-sdk/pull/13444)
- \[storage] Migrate to actor to fix a potential data race in
initialization by [@&#8203;paulb777](https://togithub.com/paulb777) in
[https://github.com/firebase/firebase-ios-sdk/pull/13428](https://togithub.com/firebase/firebase-ios-sdk/pull/13428)
- \[Storage] Addressing Swift 6 issues with `Storage`'s instance
management by [@&#8203;ncooke3](https://togithub.com/ncooke3) in
[https://github.com/firebase/firebase-ios-sdk/pull/13445](https://togithub.com/firebase/firebase-ios-sdk/pull/13445)
- Functions Cleanup by
[@&#8203;yakovmanshin](https://togithub.com/yakovmanshin) in
[https://github.com/firebase/firebase-ios-sdk/pull/13449](https://togithub.com/firebase/firebase-ios-sdk/pull/13449)
- \[Infra] Removing 'release.yml' special casing in
'scripts/release_testing_setup.sh' by
[@&#8203;ncooke3](https://togithub.com/ncooke3) in
[https://github.com/firebase/firebase-ios-sdk/pull/13447](https://togithub.com/firebase/firebase-ios-sdk/pull/13447)
- \[Storage] Manage fetcherService from a data race safe singleton by
[@&#8203;paulb777](https://togithub.com/paulb777) in
[https://github.com/firebase/firebase-ios-sdk/pull/13446](https://togithub.com/firebase/firebase-ios-sdk/pull/13446)
- Update to xcodeproj 1.25.0 by
[@&#8203;paulb777](https://togithub.com/paulb777) in
[https://github.com/firebase/firebase-ios-sdk/pull/13462](https://togithub.com/firebase/firebase-ios-sdk/pull/13462)
- Bump rexml from 3.2.8 to 3.3.3 in /.github/actions/notices_generation
by [@&#8203;dependabot](https://togithub.com/dependabot) in
[https://github.com/firebase/firebase-ios-sdk/pull/13463](https://togithub.com/firebase/firebase-ios-sdk/pull/13463)
- \[Auth] Fix async/await crash from implicitly unwrapped nil error by
[@&#8203;ncooke3](https://togithub.com/ncooke3) in
[https://github.com/firebase/firebase-ios-sdk/pull/13472](https://togithub.com/firebase/firebase-ios-sdk/pull/13472)
- Refactored Function Calling by
[@&#8203;yakovmanshin](https://togithub.com/yakovmanshin) in
[https://github.com/firebase/firebase-ios-sdk/pull/13476](https://togithub.com/firebase/firebase-ios-sdk/pull/13476)
- Firestore VectorValue type by
[@&#8203;MarkDuckworth](https://togithub.com/MarkDuckworth) in
[https://github.com/firebase/firebase-ios-sdk/pull/13404](https://togithub.com/firebase/firebase-ios-sdk/pull/13404)
- \[CoreInternal] Address Swift 6 warnings (1) by
[@&#8203;ncooke3](https://togithub.com/ncooke3) in
[https://github.com/firebase/firebase-ios-sdk/pull/13454](https://togithub.com/firebase/firebase-ios-sdk/pull/13454)
- \[Firestore] Update Firestore SPM binary to fix spm-binary workflow by
[@&#8203;ncooke3](https://togithub.com/ncooke3) in
[https://github.com/firebase/firebase-ios-sdk/pull/13485](https://togithub.com/firebase/firebase-ios-sdk/pull/13485)
- \[Firestore] Resolve protocol conformance warnings by
[@&#8203;ncooke3](https://togithub.com/ncooke3) in
[https://github.com/firebase/firebase-ios-sdk/pull/13484](https://togithub.com/firebase/firebase-ios-sdk/pull/13484)
- \[Auth] Update sample plist to have URL scheme for phone auth by
[@&#8203;ncooke3](https://togithub.com/ncooke3) in
[https://github.com/firebase/firebase-ios-sdk/pull/13487](https://togithub.com/firebase/firebase-ios-sdk/pull/13487)
- \[Infra] Force link remaining categories after
[#&#8203;13435](https://togithub.com/firebase/firebase-ios-sdk/issues/13435)
by [@&#8203;ncooke3](https://togithub.com/ncooke3) in
[https://github.com/firebase/firebase-ios-sdk/pull/13491](https://togithub.com/firebase/firebase-ios-sdk/pull/13491)
- \[Firestore] Add Sendable annotation to VectorValue by
[@&#8203;ncooke3](https://togithub.com/ncooke3) in
[https://github.com/firebase/firebase-ios-sdk/pull/13483](https://togithub.com/firebase/firebase-ios-sdk/pull/13483)
- Analytics 11.1.0 by [@&#8203;htcgh](https://togithub.com/htcgh) in
[https://github.com/firebase/firebase-ios-sdk/pull/13492](https://togithub.com/firebase/firebase-ios-sdk/pull/13492)
- Revert "\[Auth] Update sample plist to have URL scheme for phone auth"
by [@&#8203;ncooke3](https://togithub.com/ncooke3) in
[https://github.com/firebase/firebase-ios-sdk/pull/13493](https://togithub.com/firebase/firebase-ios-sdk/pull/13493)
- Update Firestore SPM for 11.1.0 by
[@&#8203;paulb777](https://togithub.com/paulb777) in
[https://github.com/firebase/firebase-ios-sdk/pull/13497](https://togithub.com/firebase/firebase-ios-sdk/pull/13497)
- 11.1.0 Changelog update by
[@&#8203;paulb777](https://togithub.com/paulb777) in
[https://github.com/firebase/firebase-ios-sdk/pull/13500](https://togithub.com/firebase/firebase-ios-sdk/pull/13500)
- Fix and Regression Test for FirebaseUI 1199 by
[@&#8203;paulb777](https://togithub.com/paulb777) in
[https://github.com/firebase/firebase-ios-sdk/pull/13505](https://togithub.com/firebase/firebase-ios-sdk/pull/13505)

**Full Changelog**:
firebase/firebase-ios-sdk@11.0.0...11.1.0

###
[`v11.0.0`](https://togithub.com/firebase/firebase-ios-sdk/releases/tag/11.0.0):
Firebase Apple 11.0.0

[Compare
Source](https://togithub.com/firebase/firebase-ios-sdk/compare/10.29.0...11.0.0)

The Firebase Apple SDK (11.0.0) is now available. For more details, see
the [Firebase Apple SDK release
notes.](https://firebase.google.com/support/release-notes/ios#11.0.0)

To install this SDK, see [Add Firebase to your
project.](https://firebase.google.com/docs/ios/setup)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the
rebase/retry checkbox.

👻 **Immortal**: This PR will be recreated if closed unmerged. Get
[config help](https://togithub.com/renovatebot/renovate/discussions) if
that's undesired.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Renovate
Bot](https://togithub.com/renovatebot/renovate).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOC4xOC4xIiwidXBkYXRlZEluVmVyIjoiMzguNTIuMCIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==-->

Co-authored-by: cgrindel-self-hosted-renovate[bot] <139595543+cgrindel-self-hosted-renovate[bot]@users.noreply.github.com>
@firebase firebase locked and limited conversation to collaborators Sep 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants