Skip to content

Commit 118e400

Browse files
authored
Fix HTTP Race Condition (#372)
* Fix HTTP Concurrency • Fixes a relatively rare issue that would be encountered if the SDK attempted to make concurrent HTTP requests during an invalid situation, such as after the user has revoked GDPR consent • The SDK was previously using a semaphore to wait for a batch of HTTP requests to finish. • However, if the SDK immediately fails the request (synchronously), the request would fail before the SDK started waiting for the request to finish, resulting in a permanently blocked queue. • Switched to using a dispatch_group which would handle this situation better. Also added a missing GDPR consent check to the onFocus methods * Remove Debug Statement • Removes a debug statement from the previous commit
1 parent dbf373e commit 118e400

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

iOS_SDK/OneSignalSDK/Source/OneSignalClient.m

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,27 @@ - (void)executeSimultaneousRequests:(NSDictionary<NSString *, OneSignalRequest *
7272

7373
//execute on a background thread or the semaphore will block the caller thread
7474
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
75-
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
75+
dispatch_group_t group = dispatch_group_create();
7676

7777
__block NSMutableDictionary<NSString *, NSError *> *errors = [NSMutableDictionary new];
7878
__block NSMutableDictionary<NSString *, NSDictionary *> *results = [NSMutableDictionary new];
7979

8080
for (NSString *identifier in requests.allKeys) {
8181
let request = requests[identifier];
8282

83+
//use a dispatch_group instead of a semaphore, in case the failureBlock gets called synchronously
84+
//this will prevent the SDK from waiting/blocking on a request that instantly failed
85+
dispatch_group_enter(group);
8386
[self executeRequest:request onSuccess:^(NSDictionary *result) {
8487
results[identifier] = result;
85-
dispatch_semaphore_signal(semaphore);
88+
dispatch_group_leave(group);
8689
} onFailure:^(NSError *error) {
8790
errors[identifier] = error;
88-
dispatch_semaphore_signal(semaphore);
91+
dispatch_group_leave(group);
8992
}];
9093
}
9194

92-
for (int i = 0; i < requests.count; i++) {
93-
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
94-
}
95+
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
9596

9697
//requests should all be completed at this point
9798
dispatch_async(dispatch_get_main_queue(), ^{

iOS_SDK/OneSignalSDK/Source/OneSignalTracker.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ + (void) endBackgroundFocusTask {
7979

8080
+ (void)onFocus:(BOOL)toBackground {
8181

82+
// return if the user has not granted privacy permissions
83+
if ([OneSignal requiresUserPrivacyConsent])
84+
return;
85+
8286
// Prevent the onFocus to be called twice when app being terminated
8387
// - Both WillResignActive and willTerminate
8488
if (lastOnFocusWasToBackground == toBackground)

0 commit comments

Comments
 (0)