Skip to content

Fix unbalanced background task creation in GDTCCTUploader #3838

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 12, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions GoogleDataTransportCCTSupport/GDTCCTLibrary/GDTCCTUploader.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ @interface GDTCCTUploader ()
@property(nullable, nonatomic, readwrite) NSURLSessionUploadTask *currentTask;

/** If running in the background, the current background ID. */
@property(nonatomic) GDTCORBackgroundIdentifier backgroundID;
@property(nonatomic) BOOL runningInBackground;

@end

Expand All @@ -61,7 +61,6 @@ - (instancetype)init {
_uploaderQueue = dispatch_queue_create("com.google.GDTCCTUploader", DISPATCH_QUEUE_SERIAL);
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
_uploaderSession = [NSURLSession sessionWithConfiguration:config];
_backgroundID = GDTCORBackgroundIdentifierInvalid;
}
return self;
}
Expand All @@ -86,6 +85,15 @@ - (NSURL *)defaultServerURL {
}

- (void)uploadPackage:(GDTCORUploadPackage *)package {
GDTCORBackgroundIdentifier bgID = GDTCORBackgroundIdentifierInvalid;
if (_runningInBackground) {
bgID = [[GDTCORApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
if (bgID != GDTCORBackgroundIdentifierInvalid) {
[[GDTCORApplication sharedApplication] endBackgroundTask:bgID];
}
}];
}

dispatch_async(_uploaderQueue, ^{
if (self->_currentTask || self->_currentUploadPackage) {
GDTCORLogWarning(GDTCORMCWUploadFailed, @"%@",
Expand All @@ -112,9 +120,10 @@ - (void)uploadPackage:(GDTCORUploadPackage *)package {
}
pb_release(gdt_cct_LogResponse_fields, &logResponse);
[package completeDelivery];
if (self->_backgroundID != GDTCORBackgroundIdentifierInvalid) {
[[GDTCORApplication sharedApplication] endBackgroundTask:self->_backgroundID];
self->_backgroundID = GDTCORBackgroundIdentifierInvalid;

// End the background task if there was one.
if (bgID != GDTCORBackgroundIdentifierInvalid) {
[[GDTCORApplication sharedApplication] endBackgroundTask:bgID];
}
self.currentTask = nil;
self.currentUploadPackage = nil;
Expand Down Expand Up @@ -192,12 +201,21 @@ - (void)packageExpired:(GDTCORUploadPackage *)package {
#pragma mark - GDTCORLifecycleProtocol

- (void)appWillBackground:(GDTCORApplication *)app {
_backgroundID = [app beginBackgroundTaskWithExpirationHandler:^{
if (self->_backgroundID != GDTCORBackgroundIdentifierInvalid) {
[app endBackgroundTask:self->_backgroundID];
self->_backgroundID = GDTCORBackgroundIdentifierInvalid;
_runningInBackground = YES;
__block GDTCORBackgroundIdentifier bgID = [app beginBackgroundTaskWithExpirationHandler:^{
if (bgID != GDTCORBackgroundIdentifierInvalid) {
[app endBackgroundTask:bgID];
}
}];
if (bgID != GDTCORBackgroundIdentifierInvalid) {
dispatch_async(_uploaderQueue, ^{
[[GDTCORApplication sharedApplication] endBackgroundTask:bgID];
});
}
}

- (void)appWillForeground:(GDTCORApplication *)app {
_runningInBackground = NO;
}

- (void)appWillTerminate:(GDTCORApplication *)application {
Expand Down