Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible fix for issue 261 and issue 234. #262

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 27 additions & 23 deletions Mixpanel/MPWebSocket.m
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ - (void)_MP_commonInit;
_workQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL);

// Going to set a specific on the queue so we can validate we're on the work queue
dispatch_queue_set_specific(_workQueue, (__bridge void *)self, maybe_bridge(_workQueue), NULL);
dispatch_queue_set_specific(_workQueue, (__bridge void *)_workQueue, maybe_bridge(_workQueue), NULL);

_delegateDispatchQueue = dispatch_get_main_queue();
mp_dispatch_retain(_delegateDispatchQueue);
Expand All @@ -365,17 +365,13 @@ - (void)_MP_commonInit;

- (void)assertOnWorkQueue;
{
assert(dispatch_get_specific((__bridge void *)self) == maybe_bridge(_workQueue));
assert(dispatch_get_specific((__bridge void *)_workQueue) == maybe_bridge(_workQueue));
}

- (void)dealloc
{
_inputStream.delegate = nil;
_outputStream.delegate = nil;

[_inputStream close];
[_outputStream close];

[self _closeStream];

mp_dispatch_release(_workQueue);
_workQueue = NULL;

Expand Down Expand Up @@ -689,7 +685,6 @@ - (void)_failWithError:(NSError *)error;
}];

self.readyState = MPWebSocketStateClosed;
self->_selfRetain = nil;

MixpanelError(@"Failing with error %@", error.localizedDescription);

Expand Down Expand Up @@ -1059,6 +1054,24 @@ - (void)_readFrameNew;
});
}

- (void)_closeStream
{
@synchronized(self) {
[_outputStream close];
[_inputStream close];

for (NSArray *runLoop in [_scheduledRunloops copy]) {
[self unscheduleFromRunLoop:[runLoop objectAtIndex:0] forMode:[runLoop objectAtIndex:1]];
}

[_outputStream setDelegate:nil];
[_inputStream setDelegate:nil];

_inputStream = nil;
_outputStream = nil;
}
}

- (void)_pumpWriting;
{
[self assertOnWorkQueue];
Expand All @@ -1067,6 +1080,7 @@ - (void)_pumpWriting;
if (dataLength - _outputBufferOffset > 0 && _outputStream.hasSpaceAvailable) {
NSInteger bytesWritten = [_outputStream write:((const uint8_t *)_outputBuffer.bytes + _outputBufferOffset) maxLength:(dataLength - _outputBufferOffset)];
if (bytesWritten == -1) {
[self _closeStream];
[self _failWithError:[NSError errorWithDomain:MPWebSocketErrorDomain code:2145 userInfo:[NSDictionary dictionaryWithObject:@"Error writing to stream" forKey:NSLocalizedDescriptionKey]]];
return;
}
Expand All @@ -1086,23 +1100,18 @@ - (void)_pumpWriting;
!_sentClose) {
_sentClose = YES;

[_outputStream close];
[_inputStream close];


for (NSArray *runLoop in [_scheduledRunloops copy]) {
[self unscheduleFromRunLoop:[runLoop objectAtIndex:0] forMode:[runLoop objectAtIndex:1]];
}
[self _closeStream];

if (!_failed) {
[self _performDelegateBlock:^{
if ([self.delegate respondsToSelector:@selector(webSocket:didCloseWithCode:reason:wasClean:)]) {
[self.delegate webSocket:self didCloseWithCode:self->_closeCode reason:self->_closeReason wasClean:YES];
}
_selfRetain = nil;
}];
} else {
_selfRetain = nil;
}

_selfRetain = nil;
}
}

Expand Down Expand Up @@ -1431,11 +1440,6 @@ - (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode;
if (aStream.streamError) {
[self _failWithError:aStream.streamError];
} else {
if (self.readyState != MPWebSocketStateClosed) {
self.readyState = MPWebSocketStateClosed;
self->_selfRetain = nil;
}

if (!self->_sentClose && !self->_failed) {
self->_sentClose = YES;
// If we get closed in this state it's probably not clean because we should be sending this when we send messages
Expand Down