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

EXC_BAD_ACCESS in closeWithCode #314

Closed
NVentimiglia opened this issue Nov 29, 2015 · 14 comments
Closed

EXC_BAD_ACCESS in closeWithCode #314

NVentimiglia opened this issue Nov 29, 2015 · 14 comments
Assignees
Labels

Comments

@NVentimiglia
Copy link

I am attempting to connect to a web socket using this library from within a Unity3d application. Shortly after calling Open I get a crash with a EXC_BAD_ACCESS (code=1, address=0x30) on thread 1. I am not sure how to debug this. Here is the code that Xcode shows me.

- (void)closeWithCode:(NSInteger)code reason:(NSString *)reason;
{
    assert(code);
    dispatch_async(_workQueue, ^{
        if (self.readyState == SR_CLOSING || self.readyState == SR_CLOSED) {
            return;
        }

        BOOL wasConnecting = self.readyState == SR_CONNECTING;

I am crashing on the dispatch_async line.

my code is below

    SRWebSocket *ws =[[SRWebSocket alloc] initWithURLRequest:[NSURLRequest requestWithURL:url]];
    ws.delegate = self;

    [ws open];

Am I doing it wrong ?

@rohg
Copy link

rohg commented Jun 21, 2016

Same thing is happening with my code. Could somebody please help in debugging the issue.

@nlutsenko
Copy link
Contributor

Any chance you can post a full stack trace?
I might have some idea on why this is happening (readyState is not atomic and access to it is not synchronized), but would love to verify against a stack trace.
Going to roll out a PR for it anyway, so we are more thread-safe anyhow...

@rohg
Copy link

rohg commented Jun 22, 2016

Here is the link to crash on fabric http://crashes.to/s/4e4411184da

On Tue, Jun 21, 2016 at 11:00 PM, Nikita Lutsenko notifications@github.com
wrote:

Any chance you can post a full stack trace?
I might have some idea on why this is happening (readyState is not atomic
and access to it is not synchronized), but would love to verify against a
stack trace.
Going to roll out a PR for it anyway, so we are more thread-safe anyhow...


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#314 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AHtsKr6VJn3Pfag68DCwHu708tpHsdfcks5qOB_GgaJpZM4GrG5j
.

Rohan Gupta
Software Developer - Mobile

@nlutsenko
Copy link
Contributor

@rohg, ok, this is helpful... Any chance you can specify the commit from SocketRocket that you are using? I am trying to find the exact line where it crashes right now.

@erikprice
Copy link
Contributor

I'm having the same crash. It isn't easily reproducible, but I managed to capture it in Xcode a few mins ago:

screen shot 2016-06-23 at 15 09 44

This was captured from SocketRocket version 0.5.0 .

Here's the backtrace of all threads (some stack frames from the main thread elided for privacy reasons):

(lldb) bt all
  thread #1: tid = 0xe320a2, 0x05cc24e6 libobjc.A.dylib`objc_msgSend + 38, queue = 'com.apple.main-thread'
    frame #0: 0x05cc24e6 libobjc.A.dylib`objc_msgSend + 38
    frame #1: 0x00adb97f MyApp`+[RACSubscriber subscriberWithNext:error:completed:](self=RACSubscriber, _cmd="subscriberWithNext:error:completed:", next=0xbff2e370, error=0xbff2e350, completed=0xbff2e330) + 81 at RACSubscriber.m:30 [opt]
    frame #2: 0x00ad5c70 MyApp`-[RACSignal(self=0x7b1c1c80, _cmd="subscribeNext:error:completed:", nextBlock=0xbff2e370, errorBlock=0xbff2e350, completedBlock=0xbff2e330) subscribeNext:error:completed:] + 80 at RACSignal.m:301 [opt]
    frame #3: 0x00ad3dd7 MyApp`__29-[RACSignal(.block_descriptor=<unavailable>, signal=0x7b1c1c80) bind:]_block_invoke.76 + 425 at RACSignal.m:139 [opt]
    frame #4: 0x00ad4124 MyApp`__29-[RACSignal(.block_descriptor=<unavailable>, x=0x7f6e27c0) bind:]_block_invoke.113 + 128 at RACSignal.m:165 [opt]
    frame #5: 0x00adbcee MyApp`-[RACSubscriber sendNext:](self=0x79e2a300, _cmd="sendNext:", value=0x7f6e27c0) + 125 at RACSubscriber.m:72 [opt]
    frame #6: 0x00abbb2b MyApp`-[RACPassthroughSubscriber sendNext:](self=0x79e135d0, _cmd="sendNext:", value=0x7f6e27c0) + 386 at RACPassthroughSubscriber.m:74 [opt]
    frame #7: 0x00ac9a40 MyApp`__35-[RACSignal(.block_descriptor=0x7f06b510, x=0x7f6e27c0) takeUntil:]_block_invoke.520 + 35 at RACSignal+Operations.m:741 [opt]
    frame #8: 0x00adbcee MyApp`-[RACSubscriber sendNext:](self=0x7f0544f0, _cmd="sendNext:", value=0x7f6e27c0) + 125 at RACSubscriber.m:72 [opt]
    frame #9: 0x00abbb2b MyApp`-[RACPassthroughSubscriber sendNext:](self=0x79e14640, _cmd="sendNext:", value=0x7f6e27c0) + 386 at RACPassthroughSubscriber.m:74 [opt]
    frame #10: 0x00aadc94 MyApp`__84-[NSObject(.block_descriptor=0x7f0d5bb0, value=0x00000000, change=0 key/value pairs, causedByDealloc=NO, affectedOnlyLastComponent=NO) rac_valuesAndChangesForKeyPath:options:observer:]_block_invoke.48 + 319 at NSObject+RACPropertySubscribing.m:77 [opt]
    frame #11: 0x00aac30f MyApp`__69-[NSObject(.block_descriptor=<unavailable>, trampolineTarget=0x7f2d1d10, trampolineObserver=0x7f181fa0, change=0 key/value pairs) rac_observeKeyPath:options:observer:block:]_block_invoke.62 + 533 at NSObject+RACKVOWrapper.m:135 [opt]
    frame #12: 0x00abb038 MyApp`-[RACKVOTrampoline observeValueForKeyPath:ofObject:change:context:](self=<unavailable>, _cmd="observeValueForKeyPath:ofObject:change:context:", keyPath=@"socket", object=0x7f2d1d10, change=0 key/value pairs, context=<unavailable>) + 278 at RACKVOTrampoline.m:107 [opt]
    frame #13: 0x00aba7a2 MyApp`-[RACKVOProxy observeValueForKeyPath:ofObject:change:context:](self=<unavailable>, _cmd="observeValueForKeyPath:ofObject:change:context:", keyPath=@"socket", object=0x7f2d1d10, change=0 key/value pairs, context=<unavailable>) + 330 at RACKVOProxy.m:66 [opt]
    frame #14: 0x02dd3b36 Foundation`NSKVONotify + 69
    frame #15: 0x02d61a0a Foundation`NSKeyValueNotifyObserver + 365
    frame #16: 0x02d60b1a Foundation`NSKeyValueDidChange + 462
    frame #17: 0x02e4e857 Foundation`-[NSObject(NSKeyValueObservingPrivate) _changeValueForKeys:count:maybeOldValuesDict:usingBlock:] + 1027
    frame #18: 0x02d5e192 Foundation`-[NSObject(NSKeyValueObservingPrivate) _changeValueForKey:key:key:usingBlock:] + 93
    frame #19: 0x02dc50d9 Foundation`_NSSetObjectValueAndNotify + 312
    frame #20: 0x0024feff MyApp`-[BTWebSocketClient disconnect](self=0x7f2d1d10, _cmd="disconnect") + 271 at BTWebSocketClient.m:196

    (... frames #21-77 elided ...)

    frame #78: 0x000d77da MyApp`main(argc=5, argv=0xbff31810) + 138 at main.m:16
    frame #79: 0x06a87a25 libdyld.dylib`start + 1

  thread #3: tid = 0xe320e2, 0x06dc77de libsystem_kernel.dylib`kevent64 + 10, queue = 'com.apple.libdispatch-manager'
    frame #0: 0x06dc77de libsystem_kernel.dylib`kevent64 + 10
    frame #1: 0x06a51593 libdispatch.dylib`_dispatch_mgr_invoke + 257
    frame #2: 0x06a512e3 libdispatch.dylib`_dispatch_mgr_thread + 60

  thread #7: tid = 0xe320e9, 0x06dbf49a libsystem_kernel.dylib`mach_msg_trap + 10, name = 'com.apple.NSURLConnectionLoader'
    frame #0: 0x06dbf49a libsystem_kernel.dylib`mach_msg_trap + 10
    frame #1: 0x06dbe884 libsystem_kernel.dylib`mach_msg + 68
    frame #2: 0x06229d16 CoreFoundation`__CFRunLoopServiceMachPort + 214
    frame #3: 0x062290f6 CoreFoundation`__CFRunLoopRun + 1526
    frame #4: 0x06228846 CoreFoundation`CFRunLoopRunSpecific + 470
    frame #5: 0x0622865b CoreFoundation`CFRunLoopRunInMode + 123
    frame #6: 0x01f67036 CFNetwork`+[NSURLConnection(Loader) _resourceLoadLoop:] + 472
    frame #7: 0x02dbfeda Foundation`-[NSThread main] + 76
    frame #8: 0x02e752dc Foundation`__NSThread__start__ + 1346
    frame #9: 0x06d8b780 libsystem_pthread.dylib`_pthread_body + 138
    frame #10: 0x06d8b6f6 libsystem_pthread.dylib`_pthread_start + 155
    frame #11: 0x06d88f7a libsystem_pthread.dylib`thread_start + 34

* thread #8: tid = 0xe320ec, 0x06dc6726 libsystem_kernel.dylib`__select + 10, name = 'com.apple.CFSocket.private'
  * frame #0: 0x06dc6726 libsystem_kernel.dylib`__select + 10
    frame #1: 0x06273090 CoreFoundation`__CFSocketManager + 912
    frame #2: 0x06d8b780 libsystem_pthread.dylib`_pthread_body + 138
    frame #3: 0x06d8b6f6 libsystem_pthread.dylib`_pthread_start + 155
    frame #4: 0x06d88f7a libsystem_pthread.dylib`thread_start + 34

  thread #9: tid = 0xe320ef, 0x06dbf4d6 libsystem_kernel.dylib`semaphore_wait_trap + 10, queue = 'parsing queue'
    frame #0: 0x06dbf4d6 libsystem_kernel.dylib`semaphore_wait_trap + 10
    frame #1: 0x06a4dab0 libdispatch.dylib`_dispatch_semaphore_wait_slow + 298
    frame #2: 0x06a4d97d libdispatch.dylib`dispatch_semaphore_wait + 37
    frame #3: 0x15a77dca DTXConnectionServices`-[DTXMessageParser waitForMoreData:incrementalBuffer:] + 128
    frame #4: 0x15a77789 DTXConnectionServices`-[DTXMessageParser parseMessageWithExceptionHandler:] + 70
    frame #5: 0x15a7749f DTXConnectionServices`__68-[DTXMessageParser initWithMessageHandler:andParseExceptionHandler:]_block_invoke + 53
    frame #6: 0x06a449e3 libdispatch.dylib`_dispatch_call_block_and_release + 15
    frame #7: 0x06a626ed libdispatch.dylib`_dispatch_client_callout + 14
    frame #8: 0x06a4a32e libdispatch.dylib`_dispatch_queue_drain + 1065
    frame #9: 0x06a49c79 libdispatch.dylib`_dispatch_queue_invoke + 563
    frame #10: 0x06a4b722 libdispatch.dylib`_dispatch_root_queue_drain + 442
    frame #11: 0x06a4b561 libdispatch.dylib`_dispatch_worker_thread3 + 108
    frame #12: 0x06d8b25c libsystem_pthread.dylib`_pthread_wqthread + 1050
    frame #13: 0x06d88f56 libsystem_pthread.dylib`start_wqthread + 34

  thread #14: tid = 0xe320fa, 0x06dbf49a libsystem_kernel.dylib`mach_msg_trap + 10, name = 'GAIThread'
    frame #0: 0x06dbf49a libsystem_kernel.dylib`mach_msg_trap + 10
    frame #1: 0x06dbe884 libsystem_kernel.dylib`mach_msg + 68
    frame #2: 0x06229d16 CoreFoundation`__CFRunLoopServiceMachPort + 214
    frame #3: 0x062290f6 CoreFoundation`__CFRunLoopRun + 1526
    frame #4: 0x06228846 CoreFoundation`CFRunLoopRunSpecific + 470
    frame #5: 0x0622865b CoreFoundation`CFRunLoopRunInMode + 123
    frame #6: 0x02dc1ce2 Foundation`-[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 308
    frame #7: 0x02e68dcf Foundation`-[NSRunLoop(NSRunLoop) run] + 82
    frame #8: 0x00a6f850 MyApp`+[GAI threadMain:] + 70
    frame #9: 0x02dbfeda Foundation`-[NSThread main] + 76
    frame #10: 0x02e752dc Foundation`__NSThread__start__ + 1346
    frame #11: 0x06d8b780 libsystem_pthread.dylib`_pthread_body + 138
    frame #12: 0x06d8b6f6 libsystem_pthread.dylib`_pthread_start + 155
    frame #13: 0x06d88f7a libsystem_pthread.dylib`thread_start + 34

  thread #17: tid = 0xe32135, 0x06dbf49a libsystem_kernel.dylib`mach_msg_trap + 10, name = 'WebThread'
    frame #0: 0x06dbf49a libsystem_kernel.dylib`mach_msg_trap + 10
    frame #1: 0x06dbe884 libsystem_kernel.dylib`mach_msg + 68
    frame #2: 0x06229d16 CoreFoundation`__CFRunLoopServiceMachPort + 214
    frame #3: 0x062290f6 CoreFoundation`__CFRunLoopRun + 1526
    frame #4: 0x06228846 CoreFoundation`CFRunLoopRunSpecific + 470
    frame #5: 0x0622865b CoreFoundation`CFRunLoopRunInMode + 123
    frame #6: 0x09c32890 WebCore`RunWebThread(void*) + 608
    frame #7: 0x06d8b780 libsystem_pthread.dylib`_pthread_body + 138
    frame #8: 0x06d8b6f6 libsystem_pthread.dylib`_pthread_start + 155
    frame #9: 0x06d88f7a libsystem_pthread.dylib`thread_start + 34

  thread #18: tid = 0xe32224, 0x06dbf49a libsystem_kernel.dylib`mach_msg_trap + 10, name = 'AFNetworking'
    frame #0: 0x06dbf49a libsystem_kernel.dylib`mach_msg_trap + 10
    frame #1: 0x06dbe884 libsystem_kernel.dylib`mach_msg + 68
    frame #2: 0x06229d16 CoreFoundation`__CFRunLoopServiceMachPort + 214
    frame #3: 0x062290f6 CoreFoundation`__CFRunLoopRun + 1526
    frame #4: 0x06228846 CoreFoundation`CFRunLoopRunSpecific + 470
    frame #5: 0x0622865b CoreFoundation`CFRunLoopRunInMode + 123
    frame #6: 0x02dc1ce2 Foundation`-[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 308
    frame #7: 0x02e68dcf Foundation`-[NSRunLoop(NSRunLoop) run] + 82
    frame #8: 0x00925e63 MyApp`+[AFURLConnectionOperation networkRequestThreadEntryPoint:](self=BTHTTPRequestOperation, _cmd="networkRequestThreadEntryPoint:", object=0x00000000) + 235 at AFURLConnectionOperation.m:189 [opt]
    frame #9: 0x02dbfeda Foundation`-[NSThread main] + 76
    frame #10: 0x02e752dc Foundation`__NSThread__start__ + 1346
    frame #11: 0x06d8b780 libsystem_pthread.dylib`_pthread_body + 138
    frame #12: 0x06d8b6f6 libsystem_pthread.dylib`_pthread_start + 155
    frame #13: 0x06d88f7a libsystem_pthread.dylib`thread_start + 34

  thread #21: tid = 0xe32310, 0x06dbf49a libsystem_kernel.dylib`mach_msg_trap + 10, name = 'com.squareup.SocketRocket.NetworkThread'
    frame #0: 0x06dbf49a libsystem_kernel.dylib`mach_msg_trap + 10
    frame #1: 0x06dbe884 libsystem_kernel.dylib`mach_msg + 68
    frame #2: 0x06229d16 CoreFoundation`__CFRunLoopServiceMachPort + 214
    frame #3: 0x062290f6 CoreFoundation`__CFRunLoopRun + 1526
    frame #4: 0x06228846 CoreFoundation`CFRunLoopRunSpecific + 470
    frame #5: 0x0622865b CoreFoundation`CFRunLoopRunInMode + 123
    frame #6: 0x02dc1ce2 Foundation`-[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 308
    frame #7: 0x00b561c7 MyApp`-[_SRRunLoopThread main](self=<unavailable>, _cmd="main") + 267 at SRWebSocket.m:1924 [opt]
    frame #8: 0x02e752dc Foundation`__NSThread__start__ + 1346
    frame #9: 0x06d8b780 libsystem_pthread.dylib`_pthread_body + 138
    frame #10: 0x06d8b6f6 libsystem_pthread.dylib`_pthread_start + 155
    frame #11: 0x06d88f7a libsystem_pthread.dylib`thread_start + 34

  thread #23: tid = 0xe333d2, 0x06dc6d5e libsystem_kernel.dylib`__workq_kernreturn + 10
    frame #0: 0x06dc6d5e libsystem_kernel.dylib`__workq_kernreturn + 10
    frame #1: 0x06d8b34b libsystem_pthread.dylib`_pthread_wqthread + 1289
    frame #2: 0x06d88f56 libsystem_pthread.dylib`start_wqthread + 34

  thread #25: tid = 0xe333d3, 0x06dc6d5e libsystem_kernel.dylib`__workq_kernreturn + 10
    frame #0: 0x06dc6d5e libsystem_kernel.dylib`__workq_kernreturn + 10
    frame #1: 0x06d8b34b libsystem_pthread.dylib`_pthread_wqthread + 1289
    frame #2: 0x06d88f56 libsystem_pthread.dylib`start_wqthread + 34

  thread #28: tid = 0xe33867, 0x061c2c95 CoreFoundation`CFDictionaryGetValue + 37, stop reason = EXC_BAD_ACCESS (code=1, address=0x20003)
    frame #0: 0x061c2c95 CoreFoundation`CFDictionaryGetValue + 37
    frame #1: 0x02e09192016-06-23 15:19:59.686 MyApp[28788:14885026] in loadDefaultData
d Foundation`_NSSetIntValueAndNotify + 65
    frame #2: 0x00b50ab4 MyApp`__36-[SRWebSocket closeWithCode:reason:]_block_invoke(.block_descriptor=0x82c58940) + 108 at SRWebSocket.m:708 [opt]
    frame #3: 0x06a449e3 libdispatch.dylib`_dispatch_call_block_and_release + 15
    frame #4: 0x06a626ed libdispatch.dylib`_dispatch_client_callout + 14
    frame #5: 0x06a4a32e libdispatch.dylib`_dispatch_queue_drain + 1065
    frame #6: 0x06a49c79 libdispatch.dylib`_dispatch_queue_invoke + 563
    frame #7: 0x06a4b722 libdispatch.dylib`_dispatch_root_queue_drain + 442
    frame #8: 0x06a4b561 libdispatch.dylib`_dispatch_worker_thread3 + 108
    frame #9: 0x06d8b25c libsystem_pthread.dylib`_pthread_wqthread + 1050
    frame #10: 0x06d88f56 libsystem_pthread.dylib`start_wqthread + 34

@nlutsenko
Copy link
Contributor

Woo, that helps even more!
Looks like exactly what I was suspecting - looks like reading from a property, while we are trying to change the property to yet another value.
@erikprice, I suggest trying branch from #420 to see if it helps resolve the issue.

@nlutsenko
Copy link
Contributor

@erikprice, can you also share some context from [BTWebSocketClient disconnect]?

@nlutsenko
Copy link
Contributor

Also, are you guys using KVO for readyState property by any chance? I can see that it shows up in the stack trace for the main thread, but wanted to make sure that it's not another property of it.

@nlutsenko
Copy link
Contributor

Whee! I got to the same stack trace!
Indeed, when using KVO on readyState - this is where it's coming from.

Debugging and doing some extreme load things to reproduce and then validate.

@erikprice
Copy link
Contributor

erikprice commented Jun 23, 2016

Yes, we are KVO'ing the readyState. Here's the implementation of -[BTWebSocketClient disconnect]:

@property (nonatomic, readonly) SRWebSocket *socket;

- (void)disconnect {
    if (self.socket != nil) {
        self.socket.delegate = nil;
        [self.socket close];
        self.socket = nil;
    }
}

@nlutsenko
Copy link
Contributor

I got it to crash consistently now, which is a good start!
Too sad, but making it only atomic won't quite help for KVO use case.
Working on a fix ;)

@nlutsenko
Copy link
Contributor

@erikprice @NVentimiglia @rohg, just pushed an update to #420 that makes readyState fully thread safe and properly KV Observable.
Please test, verify and let me know if you run into any issues.

@nlutsenko nlutsenko self-assigned this Jun 23, 2016
@nlutsenko nlutsenko added the bug label Jun 23, 2016
@erikprice
Copy link
Contributor

Will do that now. Thanks for the quick turnaround, @nlutsenko .

@rohg
Copy link

rohg commented Jun 24, 2016

@nlutsenko Thank you for prompt help. I will check and will update if any issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants