Skip to content

Commit 3184f84

Browse files
committed
fix: not multithread safe access to cancel var
1 parent 66d26f7 commit 3184f84

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

LaunchDarkly/LaunchDarkly/LDClient.swift

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -385,20 +385,29 @@ public class LDClient {
385385
os_log("%s LDClient.identify was called with a timeout greater than %f seconds. We recommend a timeout of less than %f seconds.", log: config.logger, type: .info, self.typeName(and: #function), LDClient.longTimeoutInterval, LDClient.longTimeoutInterval)
386386
}
387387

388-
var cancel = false
388+
// Use a thread-safe way to handle cancellation/completion state
389+
let completionQueue = DispatchQueue(label: "com.launchdarkly.ldclient.identify-completion", attributes: .concurrent)
390+
var completed = false
389391

390-
DispatchQueue.global().asyncAfter(deadline: .now() + timeout) {
391-
guard !cancel else { return }
392+
let finish: (IdentifyResult) -> Void = { result in
393+
var shouldCall = false
394+
completionQueue.sync(flags: .barrier) {
395+
if !completed {
396+
completed = true
397+
shouldCall = true
398+
}
399+
}
400+
if shouldCall {
401+
completion(result)
402+
}
403+
}
392404

393-
cancel = true
394-
completion(.timeout)
405+
DispatchQueue.global().asyncAfter(deadline: .now() + timeout) {
406+
finish(.timeout)
395407
}
396408

397409
identify(context: context, useCache: useCache) { result in
398-
guard !cancel else { return }
399-
400-
cancel = true
401-
completion(result)
410+
finish(result)
402411
}
403412
}
404413

0 commit comments

Comments
 (0)