Skip to content

Commit

Permalink
Fix crash when canceling NSURLSession task
Browse files Browse the repository at this point in the history
- The crash happened when canceling the task after read/write socket was closed.
- In -checkError:, also ignore the ENOTCONN error if there was a request socket close.
- In -readHTTPResponse, also call checkError: the same way as in -receive and -writeAndFree:. This is not directly related to the crash but make the error check going the same path.

#2085
  • Loading branch information
pasin committed Mar 4, 2018
1 parent 9c7270b commit 701a2d4
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions Objective-C/Internal/Replicator/CBLWebSocket.mm
Expand Up @@ -235,10 +235,9 @@ - (void) readHTTPResponse {
completionHandler: ^(NSData* data, BOOL atEOF, NSError* error)
{
CBLLogVerbose(WebSocket, @"Received %zu bytes of HTTP response", (size_t)data.length);
if (error) {
[self didCloseWithError: error];
if ([self checkError: error])
return;
}

if (!CFHTTPMessageAppendBytes(httpResponse, (const UInt8*)data.bytes, data.length)) {
// Error reading response!
[self didCloseWithCode: kWebSocketCloseProtocolError
Expand Down Expand Up @@ -491,7 +490,7 @@ - (void)URLSession:(NSURLSession *)session


- (bool) checkError: (NSError*)error {
if (!error)
if (!error || [self ignoreError: error])
return false;

_cancelError = error;
Expand Down Expand Up @@ -522,11 +521,8 @@ - (void) didCloseWithError: (NSError*)error {
return;
_task = nil;

// We sometimes get bogus(?) ENOTCONN errors after closing the socket.
if (_requestedClose && [error my_hasDomain: NSPOSIXErrorDomain code: ENOTCONN]) {
CBLLog(WebSocket, @"CBLWebSocket ignoring %@", error.my_compactDescription);
if ([self ignoreError: error])
error = nil;
}

C4Error c4err;
if (error) {
Expand All @@ -542,6 +538,15 @@ - (void) didCloseWithError: (NSError*)error {
}


- (BOOL) ignoreError: (NSError*)error {
// We sometimes get bogus(?) ENOTCONN errors after closing the socket.
if (_requestedClose && [error my_hasDomain: NSPOSIXErrorDomain code: ENOTCONN]) {
CBLLog(WebSocket, @"CBLWebSocket ignoring %@", error.my_compactDescription);
return YES;
}
return NO;
}

#pragma mark - UTILITIES:


Expand Down

0 comments on commit 701a2d4

Please sign in to comment.