From 6a55aa83262d12568c6971e3ac1c39fdc9056288 Mon Sep 17 00:00:00 2001 From: fractalwrench Date: Thu, 16 Aug 2018 09:34:23 +0100 Subject: [PATCH 1/4] fix: prevent potential crash during app teardown If the session API client is garbage collected while sessions are enqueued to be delivered, there can be a crash when accessing self.config.apiKey from the operation queue. --- Source/BugsnagSessionTrackingApiClient.m | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Source/BugsnagSessionTrackingApiClient.m b/Source/BugsnagSessionTrackingApiClient.m index 994b94db2..c9b38f91d 100644 --- a/Source/BugsnagSessionTrackingApiClient.m +++ b/Source/BugsnagSessionTrackingApiClient.m @@ -19,8 +19,11 @@ - (NSOperation *)deliveryOperation { } - (void)deliverSessionsInStore:(BugsnagSessionFileStore *)store { + NSString *apiKey = self.config.apiKey; + NSURL *sessionURL = self.config.sessionURL; + [self.sendQueue addOperationWithBlock:^{ - if (!self.config.apiKey) { + if (!apiKey) { bsg_log_err(@"No API key set. Refusing to send sessions."); return; } @@ -41,16 +44,16 @@ - (void)deliverSessionsInStore:(BugsnagSessionFileStore *)store { if (sessionCount > 0) { NSDictionary *HTTPHeaders = @{ @"Bugsnag-Payload-Version": @"1.0", - @"Bugsnag-API-Key": self.config.apiKey, + @"Bugsnag-API-Key": apiKey, @"Bugsnag-Sent-At": [BSG_RFC3339DateTool stringFromDate:[NSDate new]] }; [self sendData:payload withPayload:[payload toJson] - toURL:self.config.sessionURL + toURL:sessionURL headers:HTTPHeaders onCompletion:^(id data, BOOL success, NSError *error) { if (success && error == nil) { - bsg_log_info(@"Sent %lu sessions to Bugsnag", (unsigned long)sessionCount); + bsg_log_info(@"Sent %lu sessions to Bugsnag", (unsigned long) sessionCount); for (NSString *fileId in fileIds) { [store deleteFileWithId:fileId]; From 0796c1d03e45a7c3bf81c92f4d5641c8824145ae Mon Sep 17 00:00:00 2001 From: fractalwrench Date: Thu, 16 Aug 2018 09:42:26 +0100 Subject: [PATCH 2/4] docs: add changelog entry --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31b7f4cad..1d0ecf306 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ Changelog ========= +## 5.X.X (TBD) + +### Bug Fixes + +* Prevent potential crash in session delivery during app teardown [#308](https://github.com/bugsnag/bugsnag-cocoa/pull/308) + ## 5.16.3 (14 Aug 2018) ### Bug Fixes From 9f02c805719bbe72b1cd808e45e0a199ef5d65a8 Mon Sep 17 00:00:00 2001 From: fractalwrench Date: Fri, 24 Aug 2018 10:33:58 +0100 Subject: [PATCH 3/4] fix: copy string in sessiontrackingapiclient --- Source/BugsnagSessionTrackingApiClient.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/BugsnagSessionTrackingApiClient.m b/Source/BugsnagSessionTrackingApiClient.m index c9b38f91d..b140ecb7d 100644 --- a/Source/BugsnagSessionTrackingApiClient.m +++ b/Source/BugsnagSessionTrackingApiClient.m @@ -19,8 +19,8 @@ - (NSOperation *)deliveryOperation { } - (void)deliverSessionsInStore:(BugsnagSessionFileStore *)store { - NSString *apiKey = self.config.apiKey; - NSURL *sessionURL = self.config.sessionURL; + NSString *apiKey = [self.config.apiKey copy]; + NSURL *sessionURL = [self.config.sessionURL copy]; [self.sendQueue addOperationWithBlock:^{ if (!apiKey) { From d287de4075a77abf60e5d348be264f78ea92e712 Mon Sep 17 00:00:00 2001 From: fractalwrench Date: Fri, 24 Aug 2018 10:56:58 +0100 Subject: [PATCH 4/4] fix: cancel all operations in api client during dealloc --- Source/BugsnagApiClient.m | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Source/BugsnagApiClient.m b/Source/BugsnagApiClient.m index a3ac73c93..8550286c2 100644 --- a/Source/BugsnagApiClient.m +++ b/Source/BugsnagApiClient.m @@ -135,6 +135,10 @@ - (NSMutableURLRequest *)prepareRequest:(NSURL *)url return request; } +- (void)dealloc { + [self.sendQueue cancelAllOperations]; +} + @end @implementation BSGDelayOperation