Skip to content

Commit

Permalink
fix: fixes race condition that could lead to multiple start completio…
Browse files Browse the repository at this point in the history
…n invocations (#429)
  • Loading branch information
tanderson-ld authored Feb 12, 2025
1 parent cb270ec commit 17d1ad4
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions LaunchDarkly/LaunchDarkly/LDClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -827,24 +827,25 @@ public class LDClient {
}

static func start(serviceFactory: ClientServiceCreating?, config: LDConfig, context: LDContext? = nil, startWaitSeconds: TimeInterval, completion: ((_ timedOut: Bool) -> Void)? = nil) {
var completed = true
var completed = false
let internalCompletedQueue: DispatchQueue = DispatchQueue(label: "TimeOutQueue")
if !config.startOnline {
start(serviceFactory: serviceFactory, config: config, context: context)
completion?(completed)
completion?(true) // offline is considered a short circuited timed out case
} else {
let startTime = Date().timeIntervalSince1970
start(serviceFactory: serviceFactory, config: config, context: context) {
internalCompletedQueue.async {
if startTime + startWaitSeconds > Date().timeIntervalSince1970 && completed {
completed = false
completion?(completed)
if startTime + startWaitSeconds > Date().timeIntervalSince1970 && !completed {
completed = true
completion?(false) // false for not timedOut
}
}
}
internalCompletedQueue.asyncAfter(deadline: .now() + startWaitSeconds) {
if completed {
completion?(completed)
if !completed {
completed = true
completion?(true) // true for timedOut
}
}
}
Expand Down

0 comments on commit 17d1ad4

Please sign in to comment.