Skip to content

Commit 9dcfb16

Browse files
Prevent crashes on updateUser (#378)
This PR does two things to prevent crashes on updateUser: 1. Avoids using `self.currentUser` on `StatsigClient` 2. Pass `storeMarker` to `fetchValuesFromNetwork` like the other markers
1 parent 49e65ce commit 9dcfb16

1 file changed

Lines changed: 16 additions & 11 deletions

File tree

Sources/Statsig/StatsigClient.swift

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ public class StatsigClient {
4848
Diagnostics.mark?.overall.start();
4949

5050
self.sdkKey = sdkKey
51-
self.currentUser = StatsigClient.normalizeUser(user, options: options)
51+
let normalizedUser = StatsigClient.normalizeUser(user, options: options)
52+
self.currentUser = normalizedUser
5253
self.statsigOptions = options ?? StatsigOptions()
53-
self.store = InternalStore(sdkKey, self.currentUser, options: statsigOptions)
54+
self.store = InternalStore(sdkKey, normalizedUser, options: statsigOptions)
5455
self.networkService = NetworkService(sdkKey: sdkKey, options: statsigOptions, store: store)
5556
Diagnostics.mark?.initialize.loggerStart.start()
5657
self.logger = EventLogger(sdkKey: sdkKey, user: currentUser, networkService: networkService)
@@ -60,7 +61,6 @@ public class StatsigClient {
6061

6162
subscribeToApplicationLifecycle()
6263

63-
let capturedUser = self.currentUser
6464
let _onComplete: (StatsigClientError?) -> Void = { [weak self, completionWithResult, completion] error in
6565
guard let self = self else {
6666
return
@@ -73,14 +73,14 @@ public class StatsigClient {
7373
self.hasInitialized = true
7474
self.lastInitializeError = error
7575

76-
self.logger.retryFailedRequests(forUser: capturedUser);
76+
self.logger.retryFailedRequests(forUser: normalizedUser);
7777

7878
Diagnostics.mark?.overall.end(
7979
success: error == nil,
8080
details: self.store.cache.getEvaluationDetails(),
8181
errorMessage: error?.message
8282
)
83-
Diagnostics.log(self.logger, user: capturedUser, context: .initialize)
83+
Diagnostics.log(self.logger, user: normalizedUser, context: .initialize)
8484

8585
self.notifyOnInitializedListeners(error)
8686
completionWithResult?(error)
@@ -91,8 +91,10 @@ public class StatsigClient {
9191
_onComplete(nil)
9292
} else {
9393
fetchValuesFromNetwork(
94+
user: normalizedUser,
9495
marker: Diagnostics.mark?.initialize.network,
9596
processMarker: Diagnostics.mark?.initialize.process,
97+
storeMarker: Diagnostics.mark?.initialize.storeRead,
9698
completion: _onComplete
9799
)
98100
}
@@ -785,14 +787,16 @@ extension StatsigClient {
785787
extension StatsigClient {
786788

787789
internal func fetchValuesFromNetwork(
790+
user: StatsigUser,
788791
marker: NetworkMarker? = nil,
789792
processMarker: InitializeStepMarker? = nil,
793+
storeMarker: InitializeStepMarker? = nil,
790794
completion: ResultCompletionBlock?
791795
) {
792-
let currentUser = self.currentUser
793-
Diagnostics.mark?.initialize.storeRead.start()
796+
let currentUser = user
797+
storeMarker?.start()
794798
let initValues = self.store.getInitializationValues(user: currentUser)
795-
Diagnostics.mark?.initialize.storeRead.end(success: true)
799+
storeMarker?.end(success: true)
796800

797801
networkService.fetchInitialValues(
798802
for: currentUser,
@@ -805,7 +809,7 @@ extension StatsigClient {
805809
if let self = self {
806810
if let error = error {
807811
self.logger.log(Event.statsigInternalEvent(
808-
user: self.currentUser,
812+
user: user,
809813
name: "fetch_values_failed",
810814
value: nil,
811815
metadata: ["error": error.message]))
@@ -869,7 +873,8 @@ extension StatsigClient {
869873
}
870874

871875
private func updateUserImpl(_ user: StatsigUser, values: [String: Any]? = nil, completion: ResultCompletionBlock? = nil) {
872-
currentUser = StatsigClient.normalizeUser(user, options: statsigOptions)
876+
let normalizedUser = StatsigClient.normalizeUser(user, options: statsigOptions)
877+
currentUser = normalizedUser
873878
store.updateUser(currentUser, values: values)
874879
logger.user = currentUser
875880

@@ -879,7 +884,7 @@ extension StatsigClient {
879884
}
880885

881886
ensureMainThread { [weak self] in
882-
self?.fetchValuesFromNetwork { [weak self, completion] error in
887+
self?.fetchValuesFromNetwork(user: normalizedUser) { [weak self, completion] error in
883888
guard let self = self else {
884889
return
885890
}

0 commit comments

Comments
 (0)