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

Crashing at -[SRWebSocket stream:handleEvent:] (SRWebSocket.m:1405) #156

Closed
marianoabdala opened this issue Mar 19, 2014 · 22 comments
Closed
Assignees

Comments

@marianoabdala
Copy link

Hi, I'm getting random but continuos (~10-15 a day) crashes on a desktop app I'm working on presently but I couldn't reproduce it myself and it's working for the vast majority of the users.

So I was wondering if somebody else is seeing this before. I'm using SRWebSocket through libPusher, in case that makes a difference.

We are using the latest (at least on CocoaPods):
pod 'SocketRocket', '0.3.1-beta2'

Thanks in advance for any help you can provide.

Crash report:

Exception Type: SIGSEGV
Exception Codes: SEGV_NOOP at 0x0
Crashed Thread: 6

Thread 6 Crashed:
0 libobjc.A.dylib 0x00007fff8d6c57a2 objc_retain + 18
1 libsystem_blocks.dylib 0x00007fff90313580 _Block_copy_internal + 308
2 libdispatch.dylib 0x00007fff90cee546 _dispatch_Block_copy + 43
3 libdispatch.dylib 0x00007fff90cf413f dispatch_async + 17
4 [MYAPP] 0x000000010ac84303 -SRWebSocket stream:handleEvent:
5 CoreFoundation 0x00007fff8d9a4d51 _signalEventSync + 385
6 CoreFoundation 0x00007fff8d9a4b98 _cfstream_solo_signalEventSync + 328
7 CoreFoundation 0x00007fff8d9a4a0f _CFStreamSignalEvent + 623
8 CFNetwork 0x00007fff8a86327e _ZN30CoreWriteStreamCFStreamSupport20coreStreamWriteEventEP17__CoreWriteStreamm + 102
9 CFNetwork 0x00007fff8a8631ed _ZN21CoreWriteStreamClient25coreStreamEventsAvailableEm + 53
10 CFNetwork 0x00007fff8a9969c5 _ZN14CoreStreamBase14_callClientNowEP16CoreStreamClient + 53
11 CFNetwork 0x00007fff8a88c678 ___ZN14CoreStreamBase34_streamSetEventAndScheduleDeliveryEmh_block_invoke + 33
12 CFNetwork 0x00007fff8a87c40c ___ZNK17CoreSchedulingSet13_performAsyncEPKcU13block_pointerFvvE_block_invoke + 25
13 CoreFoundation 0x00007fff8d92fcd4 CFArrayApplyFunction + 68
14 CFNetwork 0x00007fff8a87c2eb _ZN19RunloopBlockContext7performEv + 115
15 CFNetwork 0x00007fff8a87c193 _ZN17MultiplexerSource7performEv + 269
16 CFNetwork 0x00007fff8a87bfc2 _ZN17MultiplexerSource8_performEPv + 72
17 CoreFoundation 0x00007fff8d964731 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17
18 CoreFoundation 0x00007fff8d955ea2 CFRunLoopDoSources0 + 242
19 CoreFoundation 0x00007fff8d95562f __CFRunLoopRun + 831
20 CoreFoundation 0x00007fff8d9550b5 CFRunLoopRunSpecific + 309
21 Foundation 0x00007fff8ca68adc -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 253
22 [MYAPP] 0x000000010ac8546c -_SRRunLoopThread main
23 Foundation 0x00007fff8ca6676b __NSThread__main
+ 1318
24 libsystem_pthread.dylib 0x00007fff8a43b899 _pthread_body + 138
25 libsystem_pthread.dylib 0x00007fff8a43b72a _pthread_struct_init + 0

@jleandroperez
Copy link
Contributor

@marianoabdala hi there Mariano,

We're seeing crashlogs similar to the one you posted.

May i ask you if you found anything that might be causing this?

Thanks in advance!,
Jorge

@marianoabdala
Copy link
Author

Hi @jleandroperez, sadly we didn't.

I was hoping for a reply from the repo's author but didn't get one.

@jleandroperez
Copy link
Contributor

@marianoabdala thanks for the quick reply!. I'll dig further, and will keep you posted!

@marianoabdala
Copy link
Author

@jleandroperez I appreciate it.

@jleandroperez
Copy link
Contributor

@marianoabdala i've just sent a Pull Request with a potential fix for this crash.

Please, let me know if it helps!. I've been unable to directly trigger this crash, so far.

@mikelikespie
Copy link
Contributor

Doing some housekeeping. Please reopen if still relevant

@jleandroperez
Copy link
Contributor

@mikelikespie i'm afraid this is still relevant. Thanks in advance!

@mikelikespie mikelikespie reopened this Jul 24, 2015
@jleandroperez
Copy link
Contributor

@mikelikespie thanks for reopening sir 😄

@jtreanor
Copy link

I have continued to see these crashes, as well as other related issues. I believe I have a solution that should avoid all such issues.

I previously closed by SRWebSocket objects like this:

[self.websocket close];
self.websocket.delegate = nil; // I don't care about the result, just try to close :)
self.websocket = nil; // Or reassign to a different socket

What this means is that the socket will start to close but by the time it is finished ARC will have released the object so self gives EXC_BAD_ACCESS.
Although the crash itself is very hard to reproduce as mentioned by others, another side effect is that there are lots of memory leaks (see https://github.com/intercom/intercom-ios/issues/111). This makes sense, because the socket doesn't get a chance to clean up.

Rather than trying to add lots of safety checks, I suggest that a better solution is to eliminate the race condition entirely. i.e. Make sure your socket is strongly referenced until it fully closes.

I have put together a gist with a class that will patiently wait for your SRWebSocket to close. Here it is: https://gist.github.com/jtreanor/4c67d6cc7955fceb5ea1.

Now my socket closing looks like this:

[[SRWebSocketCloser sharedInstance] addWebSocket:self.websocket];
self.websocket = nil; // This is now safe because I know the socket will be retained

I feel like this behaviour isn't something that could be built into SocketRocket but I would love to hear anyone's thoughts on it.

Perhaps the best course of action is to clearly document that SRWebSocket objects should always be retained until fully closed (not ideal either because we all know people will miss it 😃).

@arasmussen
Copy link

I'm also seeing this crash many times a day.

dfed added a commit that referenced this issue Feb 19, 2016
Issue #156: Prevents crash in handleEvent method
@anxiaoyi
Copy link

I meet the same problem, sometimes, it crashed at

while ([_runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]) {

}

@siberianisaev
Copy link

siberianisaev commented Apr 19, 2016

Firebase

Thread : Crashed: com.squareup.SocketRocket.NetworkThread
0  libobjc.A.dylib                0x319716ba objc_retain + 9
1  libsystem_blocks.dylib         0x31f0e969 _Block_copy_internal + 296
2  libdispatch.dylib              0x31eb7a61 _dispatch_Block_copy + 20
3  libdispatch.dylib              0x31ec0e59 dispatch_async$VARIANT$mp + 56
4  Firebase                       0xb82f33 -[FSRWebSocket stream:handleEvent:] (FSRWebSocket.m:1439)
5  CoreFoundation                 0x23e1a9c7 _signalEventSync + 146
6  CoreFoundation                 0x23e1a921 _cfstream_shared_signalEventSync + 340
7  CoreFoundation                 0x23e69fd7 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 14
8  CoreFoundation                 0x23e6947d __CFRunLoopDoSources0 + 368
9  CoreFoundation                 0x23e67a69 __CFRunLoopRun + 768
10 CoreFoundation                 0x23db4b31 CFRunLoopRunSpecific + 476
11 CoreFoundation                 0x23db4943 CFRunLoopRunInMode + 106
12 Foundation                     0x24af18f1 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 264
13 Firebase                       0xb84037 -[_FSRRunLoopThread main] (FSRWebSocket.m:1791)
14 Foundation                     0x24bb693b __NSThread__main__ + 1118
15 libsystem_pthread.dylib        0x32037e67 _pthread_body + 138
16 libsystem_pthread.dylib        0x32037ddb _pthread_start + 118
17 libsystem_pthread.dylib        0x32035b84 thread_start + 8
Thread : Crashed: com.squareup.SocketRocket.NetworkThread
0  libobjc.A.dylib                0x197040174 objc_release + 20
1  Firebase                       0x100b065a8 -[FSRWebSocket stream:handleEvent:] (FSRWebSocket.m:1529)
2  CoreFoundation                 0x18575c070 _signalEventSync + 192
3  CoreFoundation                 0x18575bf8c _cfstream_shared_signalEventSync + 444
4  CoreFoundation                 0x1857bc240 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
5  CoreFoundation                 0x1857bb59c __CFRunLoopDoSources0 + 448
6  CoreFoundation                 0x1857b9594 __CFRunLoopRun + 712
7  CoreFoundation                 0x1856e52d4 CFRunLoopRunSpecific + 396
8  Foundation                     0x18661d4c8 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 316
9  Firebase                       0x100b07848 -[_FSRRunLoopThread main] (FSRWebSocket.m:1791)
10 Foundation                     0x186705db8 __NSThread__main__ + 1072
11 libsystem_pthread.dylib        0x19785bdc8 _pthread_body + 164
12 libsystem_pthread.dylib        0x19785bd24 _pthread_body + 158
13 libsystem_pthread.dylib        0x197858ef8 thread_start + 4
Thread : Crashed: com.squareup.SocketRocket.NetworkThread
0  libobjc.A.dylib                0x196adfbd0 objc_msgSend + 16
1  CoreFoundation                 0x184cd9a08 CFDictionaryGetValue + 200
2  CFNetwork                      0x184731834 URLRequest::copyProtocolPropertyForKey(__CFString const*) const + 112
3  CFNetwork                      0x184843188 -[NSURLRequest _propertyForKey:] + 40
4  Firebase                       0x100b9a3b8 -[FSRWebSocket stream:handleEvent:] (FSRWebSocket.m:1408)
5  CoreFoundation                 0x184d58070 _signalEventSync + 192
6  CoreFoundation                 0x184d57f8c _cfstream_shared_signalEventSync + 444
7  CoreFoundation                 0x184db8240 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
8  CoreFoundation                 0x184db759c __CFRunLoopDoSources0 + 448
9  CoreFoundation                 0x184db5594 __CFRunLoopRun + 712
10 CoreFoundation                 0x184ce12d4 CFRunLoopRunSpecific + 396
11 Foundation                     0x185c194c8 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 316
12 Firebase                       0x100b9b848 -[_FSRRunLoopThread main] (FSRWebSocket.m:1791)
13 Foundation                     0x185d01db8 __NSThread__main__ + 1072
14 libsystem_pthread.dylib        0x19732bdc8 _pthread_body + 164
15 libsystem_pthread.dylib        0x19732bd24 _pthread_body + 158
16 libsystem_pthread.dylib        0x197328ef8 thread_start + 4

Intercom

Crashed: io.intercom.PodIntercom_SRWebSocket.NetworkThread
0  libobjc.A.dylib                0x24f6c95c objc_initWeak + 51
1  libobjc.A.dylib                0x24f6c959 objc_initWeak + 48
2  Firecracker                    0x385627 -[PodIntercom_SRWebSocket stream:handleEvent:] + 3773991
3  CoreFoundation                 0x25702a0b _signalEventSync + 146
4  CoreFoundation                 0x2570d159 _cfstream_solo_signalEventSync + 208
5  CoreFoundation                 0x25702699 _CFStreamSignalEvent + 320
6  CFNetwork                      0x25c7c2d9 SocketStream::dispatchSignalFromSocketCallbackUnlocked(SocketStreamSignalHolder*) + 40
7  CFNetwork                      0x25c7bfb5 SocketStream::socketCallback(__CFSocket*, unsigned long, __CFData const*, void const*) + 188
8  CFNetwork                      0x25c7bec3 SocketStream::_SocketCallBack_stream(__CFSocket*, unsigned long, __CFData const*, void const*, void*) + 58
9  CoreFoundation                 0x2575b5ad __CFSocketPerformV0 + 456
10 CoreFoundation                 0x257579e7 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 14
11 CoreFoundation                 0x257575d7 __CFRunLoopDoSources0 + 454

@jtreanor
Copy link

@anxiaoyi @siberianisaev Are you seeing this crash on 0.5.0? I believe it should have been fixed in #169.

@jtreanor
Copy link

@siberianisaev If you're having an issue with Intercom, it'd be great if you could add details here: https://github.com/intercom/intercom-ios/issues/124.

@michaelkirk
Copy link
Contributor

michaelkirk commented Jun 27, 2016

Should this be closed?

Several of our top crashes used to be in handleEvent, but they have disappeared since updating from 7373546 to b0aabde.

Thank you! ❤️

@jleandroperez
Copy link
Contributor

Pure awesomeness!! glad to hear that @michaelkirk !!!

:hug:

@nlutsenko
Copy link
Contributor

Glad to hear this. Indeed there was a lot of fixes in the recent month.

Anyone still experiencing crashes - please make sure you are running latest master and open a new issue.

@8ggmaker
Copy link

8ggmaker commented Jun 6, 2017

so this fix is not in cocoapods 0.5.1? can not find this in 0.5.1

@revolutionkpi
Copy link

I have got this crash a lot of time in 0.5.1 version.

@odemolliens
Copy link

Same here

madlymad pushed a commit to madlymad/SocketRocket that referenced this issue Jul 6, 2017
@sivanmoshe
Copy link

I saw the code of the fix for this crash: I think the crash can still happen
cause _scheduleCleanup and _cleanupSelfReference are called in some places without calling first to remove from run loop, on the input and output streams.
I suggest to include remove from run loop in the cleanup function, and after closing the streams, schedule another scheduleInRunLoop which then will call - dispatch_async(_workQueue, ^{
_selfRetain = nil;
});

this way we make sure that the last request in the run loop is just releasing the selfRetain, after all possible stream events occurred.

@qingfengiOS
Copy link

I meet the same problem, sometimes, it crashed at

while ([_runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]) {

}

I have the same issues, do you know how to fix?

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

No branches or pull requests