From 8abd4edf2a48242ed80c907f583a60e4844e4f6b Mon Sep 17 00:00:00 2001 From: sivanmoshe Date: Thu, 3 Aug 2017 12:29:18 +0300 Subject: [PATCH 1/4] Update SRWebSocket.m --- SocketRocket/SRWebSocket.m | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/SocketRocket/SRWebSocket.m b/SocketRocket/SRWebSocket.m index 83f3e128f..5d110ecd1 100644 --- a/SocketRocket/SRWebSocket.m +++ b/SocketRocket/SRWebSocket.m @@ -1134,25 +1134,34 @@ - (void)_scheduleCleanup } } +- (void)removeAllFromRunLoops { + for (NSArray *runLoop in [_scheduledRunloops copy]) { + [self unscheduleFromRunLoop:[runLoop objectAtIndex:0] forMode:[runLoop objectAtIndex:1]]; + } +} + - (void)_cleanupSelfReference:(NSTimer *)timer { - @synchronized(self) { - // Nuke NSStream delegate's - _inputStream.delegate = nil; - _outputStream.delegate = nil; - - // Remove the streams, right now, from the networkRunLoop - [_inputStream close]; - [_outputStream close]; - } + [_inputStream close]; + [_outputStream close]; + + [self removeAllFromRunLoops]; + + _inputStream.delegate = nil; + _outputStream.delegate = nil; + + //this is done to make sure that last request in the loop, is for setting _selfRetain to nil + NSTimer *selfRefTimer = [NSTimer timerWithTimeInterval:(0.0f) target:self selector:@selector(releaseSelRef) userInfo:nil repeats:NO]; + [[NSRunLoop SR_networkRunLoop] addTimer:selfRefTimer forMode:NSDefaultRunLoopMode]; +} - // Cleanup selfRetain in the same GCD queue as usual +- (void)releaseSelRef { + dispatch_async(_workQueue, ^{ _selfRetain = nil; }); } - static const char CRLFCRLFBytes[] = {'\r', '\n', '\r', '\n'}; - (void)_readUntilHeaderCompleteWithCallback:(data_callback)dataHandler; From d04411338d11baa409fa6f14b7b72a8e929e9ccc Mon Sep 17 00:00:00 2001 From: sivanmoshe Date: Thu, 3 Aug 2017 13:42:37 +0300 Subject: [PATCH 2/4] Update SRWebSocket.m --- SocketRocket/SRWebSocket.m | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/SocketRocket/SRWebSocket.m b/SocketRocket/SRWebSocket.m index 5d110ecd1..e097abc5b 100644 --- a/SocketRocket/SRWebSocket.m +++ b/SocketRocket/SRWebSocket.m @@ -1072,17 +1072,7 @@ - (void)_pumpWriting; _inputStream.streamStatus != NSStreamStatusClosed) && !_sentClose) { _sentClose = YES; - - @synchronized(self) { - [_outputStream close]; - [_inputStream close]; - - - for (NSArray *runLoop in [_scheduledRunloops copy]) { - [self unscheduleFromRunLoop:[runLoop objectAtIndex:0] forMode:[runLoop objectAtIndex:1]]; - } - } - + if (!_failed) { [self.delegateController performDelegateBlock:^(id _Nullable delegate, SRDelegateAvailableMethods availableMethods) { if (availableMethods.didCloseWithCode) { @@ -1151,11 +1141,11 @@ - (void)_cleanupSelfReference:(NSTimer *)timer _outputStream.delegate = nil; //this is done to make sure that last request in the loop, is for setting _selfRetain to nil - NSTimer *selfRefTimer = [NSTimer timerWithTimeInterval:(0.0f) target:self selector:@selector(releaseSelRef) userInfo:nil repeats:NO]; + NSTimer *selfRefTimer = [NSTimer timerWithTimeInterval:(0.0f) target:self selector:@selector(releaseSelfRef) userInfo:nil repeats:NO]; [[NSRunLoop SR_networkRunLoop] addTimer:selfRefTimer forMode:NSDefaultRunLoopMode]; } -- (void)releaseSelRef { +- (void)releaseSelfRef { dispatch_async(_workQueue, ^{ _selfRetain = nil; From 348dae3f35af1ed7afa8764b76678ac388dbcc94 Mon Sep 17 00:00:00 2001 From: sivanmoshe Date: Thu, 3 Aug 2017 13:55:20 +0300 Subject: [PATCH 3/4] Update SRWebSocket.m --- SocketRocket/SRWebSocket.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SocketRocket/SRWebSocket.m b/SocketRocket/SRWebSocket.m index e097abc5b..7c1fac965 100644 --- a/SocketRocket/SRWebSocket.m +++ b/SocketRocket/SRWebSocket.m @@ -1073,6 +1073,8 @@ - (void)_pumpWriting; !_sentClose) { _sentClose = YES; + [self _scheduleCleanup]; + if (!_failed) { [self.delegateController performDelegateBlock:^(id _Nullable delegate, SRDelegateAvailableMethods availableMethods) { if (availableMethods.didCloseWithCode) { @@ -1080,8 +1082,6 @@ - (void)_pumpWriting; } }]; } - - [self _scheduleCleanup]; } } From 94b3e1923095a9b0739da9d21cf8c834e09bda3c Mon Sep 17 00:00:00 2001 From: sivanmoshe Date: Thu, 3 Aug 2017 22:01:58 +0300 Subject: [PATCH 4/4] Update SRWebSocket.m --- SocketRocket/SRWebSocket.m | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/SocketRocket/SRWebSocket.m b/SocketRocket/SRWebSocket.m index 7c1fac965..cbbfc50a9 100644 --- a/SocketRocket/SRWebSocket.m +++ b/SocketRocket/SRWebSocket.m @@ -1116,12 +1116,12 @@ - (void)_scheduleCleanup } _cleanupScheduled = YES; - - // Cleanup NSStream delegate's in the same RunLoop used by the streams themselves: - // This way we'll prevent race conditions between handleEvent and SRWebsocket's dealloc - NSTimer *timer = [NSTimer timerWithTimeInterval:(0.0f) target:self selector:@selector(_cleanupSelfReference:) userInfo:nil repeats:NO]; - [[NSRunLoop SR_networkRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode]; } + + // Cleanup NSStream delegate's in the same RunLoop used by the streams themselves: + // This way we'll prevent race conditions between handleEvent and SRWebsocket's dealloc + NSTimer *timer = [NSTimer timerWithTimeInterval:(0.0f) target:self selector:@selector(_cleanupSelfReference:) userInfo:nil repeats:NO]; + [[NSRunLoop SR_networkRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode]; } - (void)removeAllFromRunLoops {