Skip to content

Commit

Permalink
Merge pull request #657 from nonstrict-hq/fix-initialization-deadlock
Browse files Browse the repository at this point in the history
Avoid deadlock during first time initialisation
  • Loading branch information
jaredmixpanel authored Jan 30, 2025
2 parents 0258d2d + f6034fa commit 8f5439a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 9 additions & 1 deletion Sources/MixpanelInstance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ extension MixpanelInstance {
}

func unarchive() {
self.readWriteLock.write {
let didCreateIdentity = self.readWriteLock.write {
optOutStatus = MixpanelPersistence.loadOptOutStatusFlag(instanceName: self.name)
superProperties = MixpanelPersistence.loadSuperProperties(instanceName: self.name)
timedEvents = MixpanelPersistence.loadTimedEvents(instanceName: self.name)
Expand All @@ -927,6 +927,14 @@ extension MixpanelInstance {
distinctId = addPrefixToDeviceId(deviceId: anonymousId)
hadPersistedDistinctId = true
userId = nil
return true
} else {
return false
}
}

if didCreateIdentity {
self.readWriteLock.read {
MixpanelPersistence.saveIdentity(MixpanelIdentity.init(
distinctID: distinctId,
peopleDistinctID: people.distinctId,
Expand Down
2 changes: 1 addition & 1 deletion Sources/ReadWriteLock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ReadWriteLock {
closure()
}
}
func write(closure: () -> Void) {
func write<T>(closure: () -> T) -> T {
concurrentQueue.sync(flags: .barrier, execute: {
closure()
})
Expand Down

0 comments on commit 8f5439a

Please sign in to comment.