Skip to content

Commit ff76c8a

Browse files
FIRInstanceIDCheckinService: make sure tasks are not submitted to an invalidated NSURLSession (#2548)
FIRInstanceIDCheckinService: make sure tasks are not submitted to an invalidated NSURLSession (#2548)
1 parent ed2b99a commit ff76c8a

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

Example/InstanceID/Tests/FIRInstanceIDCheckinServiceTest.m

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,29 @@ - (void)testCheckinServiceAddsFirebaseUserAgentToHTTPHeader {
134134
}];
135135
}
136136

137+
- (void)testCheckinServiceFailsWithErrorAfterStopFetching {
138+
[self.checkinService stopFetching];
139+
140+
XCTestExpectation *checkinCompletionExpectation =
141+
[self expectationWithDescription:@"Checkin Completion"];
142+
143+
[self.checkinService
144+
checkinWithExistingCheckin:nil
145+
completion:^(FIRInstanceIDCheckinPreferences *preferences, NSError *error) {
146+
[checkinCompletionExpectation fulfill];
147+
XCTAssertNil(preferences);
148+
XCTAssertNotNil(error);
149+
XCTAssertEqual(error.code, kFIRInstanceIDErrorCodeRegistrarFailedToCheckIn);
150+
}];
151+
152+
[self waitForExpectationsWithTimeout:5
153+
handler:^(NSError *error) {
154+
if (error) {
155+
XCTFail(@"Checkin Timeout Error: %@", error);
156+
}
157+
}];
158+
}
159+
137160
#pragma mark - Stub
138161

139162
- (FIRInstanceIDCheckinPreferences *)stubCheckinCacheWithValidData {

Firebase/InstanceID/FIRIMessageCode.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ typedef NS_ENUM(NSInteger, FIRInstanceIDMessageCode) {
6565
kFIRInstanceIDMessageCodeService004 = 7004,
6666
kFIRInstanceIDMessageCodeService005 = 7005,
6767
kFIRInstanceIDMessageCodeService006 = 7006,
68+
kFIRIntsanceIDInvalidNetworkSession = 7007,
6869
// FIRInstanceIDCheckinStore.m
6970
// DO NOT USE 8002, 8004 - 8008
7071
kFIRInstanceIDMessageCodeCheckinStore000 = 8000,

Firebase/InstanceID/FIRInstanceIDCheckinService.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ - (void)checkinWithExistingCheckin:(FIRInstanceIDCheckinPreferences *)existingCh
7474
completion:(FIRInstanceIDDeviceCheckinCompletion)completion {
7575
_FIRInstanceIDDevAssert(completion != nil, @"completion required");
7676

77+
if (self.session == nil) {
78+
FIRInstanceIDLoggerError(kFIRIntsanceIDInvalidNetworkSession,
79+
@"Inconsistent state: NSURLSession has been invalidated");
80+
NSError *error =
81+
[NSError errorWithFIRInstanceIDErrorCode:kFIRInstanceIDErrorCodeRegistrarFailedToCheckIn];
82+
completion(nil, error);
83+
return;
84+
}
85+
7786
NSURL *url = [NSURL URLWithString:kDeviceCheckinURL];
7887
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
7988

@@ -179,6 +188,8 @@ - (void)checkinWithExistingCheckin:(FIRInstanceIDCheckinPreferences *)existingCh
179188

180189
- (void)stopFetching {
181190
[self.session invalidateAndCancel];
191+
// The session cannot be reused after invalidation. Dispose it to prevent accident reusing.
192+
self.session = nil;
182193
}
183194

184195
#pragma mark - Private

0 commit comments

Comments
 (0)