Skip to content

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

Closed
@marianoabdala

Description

@marianoabdala

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

Activity

jleandroperez

jleandroperez commented on Apr 25, 2014

@jleandroperez
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

marianoabdala commented on Apr 25, 2014

@marianoabdala
Author

Hi @jleandroperez, sadly we didn't.

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

jleandroperez

jleandroperez commented on Apr 25, 2014

@jleandroperez
Contributor

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

marianoabdala

marianoabdala commented on Apr 25, 2014

@marianoabdala
Author

@jleandroperez I appreciate it.

jleandroperez

jleandroperez commented on Apr 25, 2014

@jleandroperez
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

mikelikespie commented on Jul 24, 2015

@mikelikespie
Contributor

Doing some housekeeping. Please reopen if still relevant

jleandroperez

jleandroperez commented on Jul 24, 2015

@jleandroperez
Contributor

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

jleandroperez

jleandroperez commented on Jul 24, 2015

@jleandroperez
Contributor

@mikelikespie thanks for reopening sir 😄

jtreanor

jtreanor commented on Nov 15, 2015

@jtreanor

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

arasmussen commented on Jan 11, 2016

@arasmussen

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

4 remaining items

jtreanor

jtreanor commented on Apr 19, 2016

@jtreanor

@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

michaelkirk commented on Jun 27, 2016

@michaelkirk
Contributor

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

jleandroperez commented on Jun 27, 2016

@jleandroperez
Contributor

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

:hug:

nlutsenko

nlutsenko commented on Jun 27, 2016

@nlutsenko
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.

self-assigned this
on Jun 27, 2016
8ggmaker

8ggmaker commented on Jun 6, 2017

@8ggmaker

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

revolutionkpi

revolutionkpi commented on Jun 21, 2017

@revolutionkpi

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

odemolliens

odemolliens commented on Jul 5, 2017

@odemolliens

Same here

added a commit that references this issue on Jul 6, 2017
sivanmoshe

sivanmoshe commented on Aug 2, 2017

@sivanmoshe

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

qingfengiOS commented on Apr 16, 2020

@qingfengiOS

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @mikelikespie@michaelkirk@nlutsenko@marianoabdala@arasmussen

      Issue actions

        Crashing at -[SRWebSocket stream:handleEvent:] (SRWebSocket.m:1405) · Issue #156 · facebookincubator/SocketRocket