Skip to content

Commit 708e1e1

Browse files
committed
fix unsafe use of os_unfair_lock
1 parent c7ceee4 commit 708e1e1

File tree

4 files changed

+14
-17
lines changed

4 files changed

+14
-17
lines changed

FirebaseAuth/Sources/Swift/Auth/AuthComponent.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class AuthComponent: NSObject, Library, ComponentLifecycleMaintainer {
3333
private var instances: [String: Auth] = [:]
3434

3535
/// Lock to manage access to the instances array to avoid race conditions.
36-
private var instancesLock: os_unfair_lock = .init()
36+
private let instancesLock = OSAllocatedUnfairLock()
3737

3838
// MARK: - Initializers
3939

@@ -58,10 +58,10 @@ class AuthComponent: NSObject, Library, ComponentLifecycleMaintainer {
5858
// MARK: - AuthProvider conformance
5959

6060
@discardableResult func auth() -> Auth {
61-
os_unfair_lock_lock(&instancesLock)
61+
instancesLock.lock()
6262

6363
// Unlock before the function returns.
64-
defer { os_unfair_lock_unlock(&instancesLock) }
64+
defer { instancesLock.unlock() }
6565

6666
if let instance = instances[app.name] {
6767
return instance

FirebaseFunctions/Sources/Functions.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ enum FunctionsConstants {
5656
private static var instances: [String: [Functions]] = [:]
5757

5858
/// Lock to manage access to the instances array to avoid race conditions.
59-
private static var instancesLock: os_unfair_lock = .init()
59+
private static let instancesLock = OSAllocatedUnfairLock()
6060

6161
/// The custom domain to use for all functions references (optional).
6262
let customDomain: String?
@@ -304,10 +304,10 @@ enum FunctionsConstants {
304304
guard let app else {
305305
fatalError("`FirebaseApp.configure()` needs to be called before using Functions.")
306306
}
307-
os_unfair_lock_lock(&instancesLock)
307+
instancesLock.lock()
308308

309309
// Unlock before the function returns.
310-
defer { os_unfair_lock_unlock(&instancesLock) }
310+
defer { instancesLock.unlock() }
311311

312312
if let associatedInstances = instances[app.name] {
313313
for instance in associatedInstances {

FirebaseStorage/Sources/Storage.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,13 @@ import FirebaseCore
249249
private var instances: [String: Storage] = [:]
250250

251251
/// Lock to manage access to the instances array to avoid race conditions.
252-
private var instancesLock: os_unfair_lock = .init()
252+
private let instancesLock = OSAllocatedUnfairLock
253253

254254
private init() {}
255255

256256
func storage(app: FirebaseApp, bucket: String) -> Storage {
257-
os_unfair_lock_lock(&instancesLock)
258-
defer { os_unfair_lock_unlock(&instancesLock) }
257+
instancesLock.lock()
258+
defer { instancesLock.unlock() }
259259

260260
if let instance = instances[bucket] {
261261
return instance

FirebaseVertexAI/Sources/VertexAI.swift

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,20 +124,17 @@ public class VertexAI {
124124

125125
let apiConfig: APIConfig
126126

127+
/// Lock to manage access to the `instances` array to avoid race conditions.
128+
private static let instancesLock = OSAllocatedUnfairLock()
129+
127130
#if compiler(>=6)
128131
/// A map of active `VertexAI` instances keyed by the `FirebaseApp` name and the `location`, in
129132
/// the format `appName:location`.
130133
private nonisolated(unsafe) static var instances: [InstanceKey: VertexAI] = [:]
131-
132-
/// Lock to manage access to the `instances` array to avoid race conditions.
133-
private nonisolated(unsafe) static var instancesLock: os_unfair_lock = .init()
134134
#else
135135
/// A map of active `VertexAI` instances keyed by the `FirebaseApp` name and the `location`, in
136136
/// the format `appName:location`.
137137
private static var instances: [InstanceKey: VertexAI] = [:]
138-
139-
/// Lock to manage access to the `instances` array to avoid race conditions.
140-
private static var instancesLock: os_unfair_lock = .init()
141138
#endif
142139

143140
let location: String?
@@ -149,10 +146,10 @@ public class VertexAI {
149146
fatalError("No instance of the default Firebase app was found.")
150147
}
151148

152-
os_unfair_lock_lock(&instancesLock)
149+
instancesLock.lock()
153150

154151
// Unlock before the function returns.
155-
defer { os_unfair_lock_unlock(&instancesLock) }
152+
defer { instancesLock.unlock() }
156153

157154
let instanceKey = InstanceKey(appName: app.name, location: location, apiConfig: apiConfig)
158155
if let instance = instances[instanceKey] {

0 commit comments

Comments
 (0)