acmacalister / jetfire

WebSocket (RFC 6455) client library for iOS & OS X
Apache License 2.0
518 stars 83 forks source link

NSRunLoop Crash! #59

Open mykoma opened 7 years ago

mykoma commented 7 years ago

I have met a crash at

[self.inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [self.outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [self.inputStream open];
    [self.outputStream open];
    size_t dataLen = [data length];
    [self.outputStream write:[data bytes] maxLength:dataLen];
    while (self.isRunLoop) {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    }

The Line [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; always crash when I create many socket in a short time. Maybe there is a third sdk conflict with these above code.

And I spent half day to fix this problem , and finally I find a way to avoid this bug. The code is:

    [self.inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:@"JFRWebSocketRunLoopMode"];
    [self.outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:@"JFRWebSocketRunLoopMode"];
    [self.inputStream open];
    [self.outputStream open];
    size_t dataLen = [data length];
    [self.outputStream write:[data bytes] maxLength:dataLen];
    while (self.isRunLoop) {
        [[NSRunLoop currentRunLoop] runMode:@"JFRWebSocketRunLoopMode" beforeDate:[NSDate distantFuture]];
    }

I am not sure why this crash happend, neither not sure why my fix can works.

But I think should let you know this issue.

OlinJX commented 7 years ago

I have the same crash

krosinski commented 7 years ago

I fixed the crash on my fork (with extra refactoring and design changes), the real issue was the ability to initialize multiple connections when calling [socket connect] a few times on a disconnected socket (created state marked asynchronously).

67 Add blocking connect and move handling connection to dedicated NSThread