Skip to content

Commit 3dbcead

Browse files
authored
Merge pull request #2061 from morganchen12/gulchangelist
Invalidate non-background url sessions
2 parents bf1ce88 + b112d90 commit 3dbcead

File tree

5 files changed

+33
-25
lines changed

5 files changed

+33
-25
lines changed

Example/tvOSSample/tvOSSample/AuthViewController.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import UIKit
1616
import FirebaseAuth
1717

1818
class AuthViewController: UIViewController {
19-
2019
// MARK: - User Interface
2120

2221
/// A stackview containing all of the buttons to providers (Email, OAuth, etc).

Example/tvOSSample/tvOSSample/EmailLoginViewController.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ protocol EmailLoginDelegate {
2121
}
2222

2323
class EmailLoginViewController: UIViewController {
24-
2524
// MARK: - Public Properties
2625

2726
var delegate: EmailLoginDelegate?
@@ -65,8 +64,7 @@ class EmailLoginViewController: UIViewController {
6564

6665
// MARK: - View Controller Lifecycle
6766

68-
override func viewDidLoad() {
69-
}
67+
override func viewDidLoad() {}
7068

7169
// MARK: - Helper Methods
7270

GoogleUtilities/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Unreleased
2+
- Fixed an issue where GoogleUtilities would leak non-background URL sessions.
3+
(#2061)
24

35
# 5.3.4
46
- Fixed a crash caused by unprotected access to sessions in

GoogleUtilities/Network/GULNetworkURLSession.m

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
#import "Private/GULNetworkConstants.h"
2222
#import "Private/GULNetworkMessageCode.h"
2323

24+
@interface GULNetworkURLSession () <NSURLSessionDelegate,
25+
NSURLSessionTaskDelegate,
26+
NSURLSessionDownloadDelegate>
27+
@end
28+
2429
@implementation GULNetworkURLSession {
2530
/// The handler to be called when the request completes or error has occurs.
2631
GULNetworkURLSessionCompletionHandler _completionHandler;
@@ -45,6 +50,9 @@ @implementation GULNetworkURLSession {
4550

4651
/// The current request.
4752
NSURLRequest *_request;
53+
54+
/// The current NSURLSession.
55+
NSURLSession *__weak _Nullable _URLSession;
4856
}
4957

5058
#pragma mark - Init
@@ -128,7 +136,7 @@ - (NSString *)sessionIDFromAsyncPOSTRequest:(NSURLRequest *)request
128136

129137
if (didWriteFile) {
130138
// Exclude this file from backing up to iTunes. There are conflicting reports that excluding
131-
// directory from backing up does not excluding files of that directory from backing up.
139+
// directory from backing up does not exclude files of that directory from backing up.
132140
[self excludeFromBackupForURL:_uploadingFileURL];
133141

134142
_sessionConfig = [self backgroundSessionConfigWithSessionID:_sessionID];
@@ -141,7 +149,6 @@ - (NSString *)sessionIDFromAsyncPOSTRequest:(NSURLRequest *)request
141149
// If we cannot write to file, just send it in the foreground.
142150
_sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
143151
[self populateSessionConfig:_sessionConfig withRequest:request];
144-
_sessionConfig.URLCache = nil;
145152
session = [NSURLSession sessionWithConfiguration:_sessionConfig
146153
delegate:self
147154
delegateQueue:[NSOperationQueue mainQueue]];
@@ -157,6 +164,8 @@ - (NSString *)sessionIDFromAsyncPOSTRequest:(NSURLRequest *)request
157164
return nil;
158165
}
159166

167+
_URLSession = session;
168+
160169
// Save the session into memory.
161170
[[self class] setSessionInFetcherMap:self forSessionID:_sessionID];
162171

@@ -199,6 +208,8 @@ - (NSString *)sessionIDFromAsyncGETRequest:(NSURLRequest *)request
199208
return nil;
200209
}
201210

211+
_URLSession = session;
212+
202213
// Save the session into memory.
203214
[[self class] setSessionInFetcherMap:self forSessionID:_sessionID];
204215

@@ -283,16 +294,15 @@ - (void)URLSession:(NSURLSession *)session
283294
[self maybeRemoveTempFilesAtURL:_networkDirectoryURL
284295
expiringTime:kGULNetworkTempFolderExpireTime];
285296

286-
// Invalidate the session only if it's owned by this class.
287-
NSString *sessionID = session.configuration.identifier;
288-
if ([sessionID hasPrefix:kGULNetworkBackgroundSessionConfigIDPrefix]) {
289-
[session finishTasksAndInvalidate];
297+
// This is called without checking the sessionID here since non-background sessions
298+
// won't have an ID.
299+
[session finishTasksAndInvalidate];
290300

291-
// Explicitly remove the session so it won't be reused. The weak map table should
292-
// remove the session on deallocation, but dealloc may not happen immediately after
293-
// calling `finishTasksAndInvalidate`.
294-
[[self class] setSessionInFetcherMap:nil forSessionID:sessionID];
295-
}
301+
// Explicitly remove the session so it won't be reused. The weak map table should
302+
// remove the session on deallocation, but dealloc may not happen immediately after
303+
// calling `finishTasksAndInvalidate`.
304+
NSString *sessionID = session.configuration.identifier;
305+
[[self class] setSessionInFetcherMap:nil forSessionID:sessionID];
296306
}
297307

298308
- (void)URLSession:(NSURLSession *)session
@@ -677,13 +687,13 @@ + (void)setSessionInFetcherMap:(GULNetworkURLSession *)session forSessionID:(NSS
677687
GULNetworkURLSession *existingSession =
678688
[[[self class] sessionIDToFetcherMap] objectForKey:sessionID];
679689
if (existingSession) {
680-
// Invalidating doesn't seem like the right thing to do here since it may cancel an active
681-
// background transfer if the background session is handling multiple requests. The old
682-
// session will be dropped from the map table, but still complete its request.
683-
NSString *message = [NSString stringWithFormat:@"Discarding session: %@", existingSession];
684-
[existingSession->_loggerDelegate GULNetwork_logWithLevel:kGULNetworkLogLevelInfo
685-
messageCode:kGULNetworkMessageCodeURLSession019
686-
message:message];
690+
if (session) {
691+
NSString *message = [NSString stringWithFormat:@"Discarding session: %@", existingSession];
692+
[existingSession->_loggerDelegate GULNetwork_logWithLevel:kGULNetworkLogLevelInfo
693+
messageCode:kGULNetworkMessageCodeURLSession019
694+
message:message];
695+
}
696+
[existingSession->_URLSession finishTasksAndInvalidate];
687697
}
688698
if (session) {
689699
[[[self class] sessionIDToFetcherMap] setObject:session forKey:sessionID];

GoogleUtilities/Network/Private/GULNetworkURLSession.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,13 @@ typedef void (^GULNetworkURLSessionCompletionHandler)(NSHTTPURLResponse *respons
2828
typedef void (^GULNetworkSystemCompletionHandler)(void);
2929

3030
/// The protocol that uses NSURLSession for iOS >= 7.0 to handle requests and responses.
31-
@interface GULNetworkURLSession
32-
: NSObject <NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDownloadDelegate>
31+
@interface GULNetworkURLSession : NSObject
3332

3433
/// Indicates whether the background network is enabled. Default value is NO.
3534
@property(nonatomic, getter=isBackgroundNetworkEnabled) BOOL backgroundNetworkEnabled;
3635

3736
/// The logger delegate to log message, errors or warnings that occur during the network operations.
38-
@property(nonatomic, weak) id<GULNetworkLoggerDelegate> loggerDelegate;
37+
@property(nonatomic, weak, nullable) id<GULNetworkLoggerDelegate> loggerDelegate;
3938

4039
/// Calls the system provided completion handler after the background session is finished.
4140
+ (void)handleEventsForBackgroundURLSessionID:(NSString *)sessionID

0 commit comments

Comments
 (0)