diff --git a/Bugsnag/Delivery/BugsnagSessionTrackingApiClient.m b/Bugsnag/Delivery/BugsnagSessionTrackingApiClient.m index f709f9aa2..b087b6c46 100644 --- a/Bugsnag/Delivery/BugsnagSessionTrackingApiClient.m +++ b/Bugsnag/Delivery/BugsnagSessionTrackingApiClient.m @@ -50,12 +50,15 @@ - (void)deliverSessionsInStore:(BugsnagSessionFileStore *)store { NSDictionary *filesWithIds = [store allFilesByName]; for (NSString *fileId in [filesWithIds allKeys]) { - // skip dupe requests - if ([self isActiveRequest:fileId]) { - continue; + + // De-duplicate files as deletion of the file is asynchronous and so multiple calls + // to this method will result in multiple send requests + @synchronized (self.activeIds) { + if ([self.activeIds containsObject:fileId]) { + continue; + } + [self.activeIds addObject:fileId]; } - // add request - [self.activeIds addObject:fileId]; BugsnagSession *session = [[BugsnagSession alloc] initWithDictionary:filesWithIds[fileId]]; @@ -64,7 +67,6 @@ - (void)deliverSessionsInStore:(BugsnagSessionFileStore *)store { initWithSessions:@[session] config:[Bugsnag configuration] codeBundleId:self.codeBundleId]; - NSUInteger sessionCount = payload.sessions.count; NSMutableDictionary *data = [payload toJson]; NSDictionary *HTTPHeaders = @{ @"Bugsnag-Payload-Version": @"1.0", @@ -77,26 +79,19 @@ - (void)deliverSessionsInStore:(BugsnagSessionFileStore *)store { headers:HTTPHeaders onCompletion:^(NSUInteger sentCount, BOOL success, NSError *error) { if (success && error == nil) { - bsg_log_info(@"Sent %lu sessions to Bugsnag", (unsigned long) sessionCount); + bsg_log_info(@"Sent session %@ to Bugsnag", session.id); [store deleteFileWithId:fileId]; } else { bsg_log_warn(@"Failed to send sessions to Bugsnag: %@", error); } // remove request - [self.activeIds removeObject:fileId]; + @synchronized (self.activeIds) { + [self.activeIds removeObject:fileId]; + } }]; }]; } } -- (BOOL)isActiveRequest:(NSString *)fileId { - for (NSString *val in self.activeIds) { - if ([val isEqualToString:fileId]) { - return true; - } - } - return false; -} - @end diff --git a/CHANGELOG.md b/CHANGELOG.md index fc52938e4..0ff7f6dce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ Changelog ========= +## TBD + +### Bug fixes + +* Synchronize access to sessions being delivered in de-duplication code + [#756](https://github.com/bugsnag/bugsnag-cocoa/pull/756) + ## 6.1.1 (2020-07-16) ### Bug fixes