Skip to content

Commit 97745d5

Browse files
committed
Invalidate non-background url sessions
1 parent c2fd8d1 commit 97745d5

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

GoogleUtilities/Network/GULNetworkURLSession.m

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

24+
@interface GULNetworkURLSession () <NSURLSessionDelegate, NSURLSessionTaskDelegate,
25+
NSURLSessionDownloadDelegate>
26+
@end
27+
2428
@implementation GULNetworkURLSession {
2529
/// The handler to be called when the request completes or error has occurs.
2630
GULNetworkURLSessionCompletionHandler _completionHandler;
@@ -45,6 +49,9 @@ @implementation GULNetworkURLSession {
4549

4650
/// The current request.
4751
NSURLRequest *_request;
52+
53+
/// The current NSURLSession.
54+
NSURLSession *__weak _Nullable _URLSession;
4855
}
4956

5057
#pragma mark - Init
@@ -128,7 +135,7 @@ - (NSString *)sessionIDFromAsyncPOSTRequest:(NSURLRequest *)request
128135

129136
if (didWriteFile) {
130137
// 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.
138+
// directory from backing up does not exclude files of that directory from backing up.
132139
[self excludeFromBackupForURL:_uploadingFileURL];
133140

134141
_sessionConfig = [self backgroundSessionConfigWithSessionID:_sessionID];
@@ -141,7 +148,6 @@ - (NSString *)sessionIDFromAsyncPOSTRequest:(NSURLRequest *)request
141148
// If we cannot write to file, just send it in the foreground.
142149
_sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
143150
[self populateSessionConfig:_sessionConfig withRequest:request];
144-
_sessionConfig.URLCache = nil;
145151
session = [NSURLSession sessionWithConfiguration:_sessionConfig
146152
delegate:self
147153
delegateQueue:[NSOperationQueue mainQueue]];
@@ -199,6 +205,8 @@ - (NSString *)sessionIDFromAsyncGETRequest:(NSURLRequest *)request
199205
return nil;
200206
}
201207

208+
_URLSession = session;
209+
202210
// Save the session into memory.
203211
[[self class] setSessionInFetcherMap:self forSessionID:_sessionID];
204212

@@ -283,16 +291,16 @@ - (void)URLSession:(NSURLSession *)session
283291
[self maybeRemoveTempFilesAtURL:_networkDirectoryURL
284292
expiringTime:kGULNetworkTempFolderExpireTime];
285293

286-
// Invalidate the session only if it's owned by this class.
287294
NSString *sessionID = session.configuration.identifier;
288-
if ([sessionID hasPrefix:kGULNetworkBackgroundSessionConfigIDPrefix]) {
289-
[session finishTasksAndInvalidate];
290295

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-
}
296+
// This is called without checking the sessionID here since non-background sessions
297+
// won't have an ID.
298+
[session finishTasksAndInvalidate];
299+
300+
// Explicitly remove the session so it won't be reused. The weak map table should
301+
// remove the session on deallocation, but dealloc may not happen immediately after
302+
// calling `finishTasksAndInvalidate`.
303+
[[self class] setSessionInFetcherMap:nil forSessionID:sessionID];
296304
}
297305

298306
- (void)URLSession:(NSURLSession *)session
@@ -677,13 +685,13 @@ + (void)setSessionInFetcherMap:(GULNetworkURLSession *)session forSessionID:(NSS
677685
GULNetworkURLSession *existingSession =
678686
[[[self class] sessionIDToFetcherMap] objectForKey:sessionID];
679687
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];
688+
if (session != nil) {
689+
NSString *message = [NSString stringWithFormat:@"Discarding session: %@", existingSession];
690+
[existingSession->_loggerDelegate GULNetwork_logWithLevel:kGULNetworkLogLevelInfo
691+
messageCode:kGULNetworkMessageCodeURLSession019
692+
message:message];
693+
}
694+
[existingSession->_URLSession finishTasksAndInvalidate];
687695
}
688696
if (session) {
689697
[[[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)