nuclearace / Socket.IO-Client-Swift

socket.io-client for Swift
Other
361 stars 53 forks source link

Reconnect crashes the app upon disconnect #62

Closed piofficetwo closed 9 years ago

piofficetwo commented 9 years ago

I am not very good with IOS programming so bare with me. I am not sure what is happening, but when I disconnect the server, the client tries to reconnect and this crashes the app with this:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Swift._NSContiguousString objectForKey:]: unrecognized selector sent to instance 0x7fa83b8715c0'
*** First throw call stack:
(
0   CoreFoundation                      0x0000000105a8ec65 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x0000000105279bb7 objc_exception_throw + 45
2   CoreFoundation                      0x0000000105a960ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3   CoreFoundation                      0x00000001059ec13c ___forwarding___ + 988
4   CoreFoundation                      0x00000001059ebcd8 _CF_forwarding_prep_0 + 120
5   hermeschat                          0x000000010203582e __40-[GMMessagingManager initiateConnection]_block_invoke + 270
6   hermeschat                          0x00000001020dd292 _TTRXFdCb_dGSQCSo8NSString_dGSqCSo7NSArray__dT__XFo_oGSQSS_oGSqS0___dT__ + 146
7   hermeschat                          0x00000001020dd1d3 _TFFC10hermeschat14ExtendedSocket5onAnyFS0_FFTGSQSS_GSqCSo7NSArray__T_T_U_FCS_14SocketAnyEventT_ + 2019
8   hermeschat                          0x00000001020ce477 _TTRXFo_oC10hermeschat14SocketAnyEvent_dT__XFo_iS0__iT__ + 23
9   hermeschat                          0x00000001020caa91 _TPA__TTRXFo_oC10hermeschat14SocketAnyEvent_dT__XFo_iS0__iT__ + 81
10  hermeschat                          0x00000001020ce070 _TTRXFo_iC10hermeschat14SocketAnyEvent_iT__XFo_oS0__dT__ + 32
11  hermeschat                          0x00000001020ce004 _TFFC10hermeschat14SocketIOClient11handleEventFS0_FTSS4dataGSqGSaPSs9AnyObject___17isInternalMessageSb8wantsAckGSqSi__T_U_FT_T_ + 484
12  hermeschat                          0x00000001020cca67 _TTRXFo__dT__XFdCb__dT__ + 39
13  libdispatch.dylib                   0x000000010715b186 _dispatch_call_block_and_release + 12
14  libdispatch.dylib                   0x000000010717a614 _dispatch_client_callout + 8
15  libdispatch.dylib                   0x0000000107162a1c _dispatch_main_queue_callback_4CF + 1664
16  CoreFoundation                      0x00000001059f61f9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
17  CoreFoundation                      0x00000001059b7dcb __CFRunLoopRun + 2043
18  CoreFoundation                      0x00000001059b7366 CFRunLoopRunSpecific + 470
19  GraphicsServices                    0x000000010799fa3e GSEventRunModal + 161
20  UIKit                               0x0000000103b07900 UIApplicationMain + 1282
21  hermeschat                          0x0000000101fd99ef main + 111
22  libdyld.dylib                       0x00000001071ae145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

Yet, when I comment these two lines out:

        handleEvent("reconnect", data: [reason], isInternalMessage: true)
        tryReconnect()'

The app does not crash. Any idea on why this is happening?

nuclearace commented 9 years ago

How are you disconnecting the client?

piofficetwo commented 9 years ago

I am disconnecting the client by shutting down the server. The server is running on nodejs.

nuclearace commented 9 years ago

Not sure if it matters, but are you using this from Objective-C?

nuclearace commented 9 years ago

Also does this always happen?

piofficetwo commented 9 years ago

I am using objective-C but I don't think it is related to socketIO. I am wrapping around the swift library and then using this wrapped library in my objective-C class. However, I am not dealing with the reconnection at any point.

This happens 100% of the time with the exact same error.

If I just comment out this line:

handleEvent("reconnect", data: [reason], isInternalMessage: true)

Then the app crashes here:

handleEvent("reconnectAttempt", data: [reconnectAttempts - currentReconnectAttempt],
        isInternalMessage: true)

I think this line is crashing the program:

image

What is interesting is that this error happens once in a while upon some other events as well, but for those events, the crash is inconsistent and only happens once every 10-20 times.

EDIT: Actually now thinking about it, I think it only crashes on connection for events that are not reconnects

nuclearace commented 9 years ago

Hm, I'm having trouble reproducing from Swift. Shutting down the server while it's connected doesn't seem to cause any crashes for me.

piofficetwo commented 9 years ago

Do you know why this might be happening? Perhaps I am doing something wrong but currently I really have no idea why it might fail

nuclearace commented 9 years ago

My objective-c test project doesn't seem to crash either. You said you're wrapping around the library. What does that mean?

piofficetwo commented 9 years ago

I am trying to make my program resend data up to 5 times if the client does not receive the data. I want this to apply to many different scenarios so I wrapped the socket and made the behaviour so that it resends it up to 5 times until a confirmation is received.

Just wondering, if I use "onAny", does this interfere with anything such as the reconnect?

nuclearace commented 9 years ago

I would think that the client should always get the data, since it uses TCP. And onAny shouldn't affect anything.

piofficetwo commented 9 years ago

I got the error! It has nothing to do with the library so don't worry.

Thanks for your help btw!