Skip to content

Commit

Permalink
Merge pull request facebookincubator#414 from facebook/nlutsenko.prox…
Browse files Browse the repository at this point in the history
…y.crash

Fix crash on SRProxyConnect deallocation, when timeout was encountered.
  • Loading branch information
nlutsenko committed Jun 17, 2016
2 parents e17d8ce + 14d35ac commit aaab483
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions SocketRocket/Internal/Proxy/SRProxyConnect.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ @implementation SRProxyConnect
BOOL _connectionRequiresSSL;

NSMutableArray<NSData *> *_inputQueue;
NSOperationQueue * _writeQueue;
dispatch_queue_t _writeQueue;
}

///--------------------------------------
Expand All @@ -54,12 +54,26 @@ -(instancetype)initWithURL:(NSURL *)url
_url = url;
_connectionRequiresSSL = SRURLRequiresSSL(url);

_writeQueue = [[NSOperationQueue alloc] init];
_writeQueue = dispatch_queue_create("com.facebook.socketrocket.proxyconnect.write", DISPATCH_QUEUE_SERIAL);
_inputQueue = [NSMutableArray arrayWithCapacity:2];

return self;
}

- (void)dealloc
{
// If we get deallocated before the socket open finishes - we need to cleanup everything.

[self.inputStream removeFromRunLoop:[NSRunLoop SR_networkRunLoop] forMode:NSDefaultRunLoopMode];
self.inputStream.delegate = nil;
[self.inputStream close];
self.inputStream = nil;

self.outputStream.delegate = nil;
[self.outputStream close];
self.outputStream = nil;
}

///--------------------------------------
#pragma mark - Open
///--------------------------------------
Expand Down Expand Up @@ -113,6 +127,10 @@ - (void)_failWithError:(NSError *)error
CFRelease(_receivedHTTPHeaders);
_receivedHTTPHeaders = NULL;
}

self.inputStream.delegate = nil;
self.outputStream.delegate = nil;

[self.inputStream removeFromRunLoop:[NSRunLoop SR_networkRunLoop]
forMode:NSDefaultRunLoopMode];
[self.inputStream close];
Expand Down Expand Up @@ -433,7 +451,7 @@ - (void)_writeData:(NSData *)data
const uint8_t * bytes = data.bytes;
__block NSInteger timeout = SRProxyConnectWriteTimeout * 1000000; // wait timeout before giving up
__weak typeof(self) wself = self;
[_writeQueue addOperationWithBlock:^() {
dispatch_async(_writeQueue, ^{
if (!wself) {
return;
}
Expand All @@ -452,7 +470,7 @@ - (void)_writeData:(NSData *)data
}
}
[outStream write:bytes maxLength:data.length];
}];
});
}
@end

Expand Down

0 comments on commit aaab483

Please sign in to comment.