Browse Source

Merge pull request #455 from facebook/nlutsenko.proxy.crash

Optimize input queue processing in SRProxyConnect.
Nikita Lutsenko 9 years ago
parent
commit
00a8a66240
2 changed files with 15 additions and 5 deletions
  1. 10 3
      SocketRocket/Internal/Proxy/SRProxyConnect.m
  2. 5 2
      SocketRocket/SRWebSocket.m

+ 10 - 3
SocketRocket/Internal/Proxy/SRProxyConnect.m

@@ -406,13 +406,17 @@
 - (void)_dequeueInput
 - (void)_dequeueInput
 {
 {
     while (_inputQueue.count > 0) {
     while (_inputQueue.count > 0) {
-        NSData *data = _inputQueue[0];
-        [self _proxyProcessHTTPResponseWithData:data];
+        NSData *data = _inputQueue.firstObject;
         [_inputQueue removeObjectAtIndex:0];
         [_inputQueue removeObjectAtIndex:0];
+
+        // No need to process any data further, we got the full header data.
+        if ([self _proxyProcessHTTPResponseWithData:data]) {
+            break;
+        }
     }
     }
 }
 }
 //handle checking the proxy  connection status
 //handle checking the proxy  connection status
-- (void)_proxyProcessHTTPResponseWithData:(NSData *)data
+- (BOOL)_proxyProcessHTTPResponseWithData:(NSData *)data
 {
 {
     if (_receivedHTTPHeaders == NULL) {
     if (_receivedHTTPHeaders == NULL) {
         _receivedHTTPHeaders = CFHTTPMessageCreateEmpty(NULL, NO);
         _receivedHTTPHeaders = CFHTTPMessageCreateEmpty(NULL, NO);
@@ -422,7 +426,10 @@
     if (CFHTTPMessageIsHeaderComplete(_receivedHTTPHeaders)) {
     if (CFHTTPMessageIsHeaderComplete(_receivedHTTPHeaders)) {
         SRDebugLog(@"Finished reading headers %@", CFBridgingRelease(CFHTTPMessageCopyAllHeaderFields(_receivedHTTPHeaders)));
         SRDebugLog(@"Finished reading headers %@", CFBridgingRelease(CFHTTPMessageCopyAllHeaderFields(_receivedHTTPHeaders)));
         [self _proxyHTTPHeadersDidFinish];
         [self _proxyHTTPHeadersDidFinish];
+        return YES;
     }
     }
+
+    return NO;
 }
 }
 
 
 - (void)_proxyHTTPHeadersDidFinish
 - (void)_proxyHTTPHeadersDidFinish

+ 5 - 2
SocketRocket/SRWebSocket.m

@@ -326,8 +326,6 @@ NSString *const SRHTTPResponseErrorKey = @"HTTPResponseStatusCode";
 
 
 - (void)_connectionDoneWithError:(NSError *)error readStream:(NSInputStream *)readStream writeStream:(NSOutputStream *)writeStream
 - (void)_connectionDoneWithError:(NSError *)error readStream:(NSInputStream *)readStream writeStream:(NSOutputStream *)writeStream
 {
 {
-    _proxyConnect = nil; // Job's done! This is not longer required.
-
     if (error != nil) {
     if (error != nil) {
         [self _failWithError:error];
         [self _failWithError:error];
     } else {
     } else {
@@ -350,6 +348,11 @@ NSString *const SRHTTPResponseErrorKey = @"HTTPResponseStatusCode";
             });
             });
         }
         }
     }
     }
+    // Schedule to run on a work queue, to make sure we don't run this inline and deallocate `self` inside `SRProxyConnect`.
+    // TODO: (nlutsenko) Find a better structure for this, maybe Bolts Tasks?
+    dispatch_async(_workQueue, ^{
+        _proxyConnect = nil;
+    });
 }
 }
 
 
 - (BOOL)_checkHandshake:(CFHTTPMessageRef)httpMessage;
 - (BOOL)_checkHandshake:(CFHTTPMessageRef)httpMessage;