davidstump / SwiftPhoenixClient

Connect your Phoenix and iOS applications through WebSockets!
MIT License
507 stars 147 forks source link

Crash in HeartbeatTimer.swift #188

Closed Gamec closed 3 years ago

Gamec commented 3 years ago

After updating to SwiftPhoenixClient 2.0 (happens in 2.1 too) I started getting crash reports from HeartbeatTimer. They are pretty rare, I didn't manage to reproduce it myself. Maybe you have any idea why they are happening?

This is line 74 crashing:

Crashed: NSOperationQueue 0x120417610 (QOS: UNSPECIFIED)
0  libswiftCore.dylib             0x19a4a6c88 _swift_release_dealloc + 24
1  SwiftPhoenixClient             0x1034f6394 HeartbeatTimer.stopTimer() + 74 (HeartbeatTimer.swift:74)
2  SwiftPhoenixClient             0x1035083bc specialized Socket.onConnectionClosed(code:) + 597 (Socket.swift:597)
3  SwiftPhoenixClient             0x103506ad4 protocol witness for PhoenixTransportDelegate.onClose(code:) in conformance Socket + 4414728916 (<compiler-generated>:4414728916)
4  SwiftPhoenixClient             0x1034f892c URLSessionTransport.abnormalErrorReceived(_:) + 291 (PhoenixTransport.swift:291)
5  SwiftPhoenixClient             0x1034f82cc @objc URLSessionTransport.urlSession(_:task:didCompleteWithError:) + 4414669516 (<compiler-generated>:4414669516)
6  CFNetwork                      0x196da55c0 _CFNetworkHTTPConnectionCacheSetLimit + 152760
7  Foundation                     0x197950fa8 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 24
8  Foundation                     0x19783e878 -[NSBlockOperation main] + 104
9  Foundation                     0x1979533d0 __NSOPERATION_IS_INVOKING_MAIN__ + 24
10 Foundation                     0x19783e500 -[NSOperation start] + 808
11 Foundation                     0x197953e74 __NSOPERATIONQUEUE_IS_STARTING_AN_OPERATION__ + 24
12 Foundation                     0x197953900 __NSOQSchedule_f + 184
13 libdispatch.dylib              0x1961c2484 _dispatch_block_async_invoke2 + 148
14 libdispatch.dylib              0x1961b381c _dispatch_client_callout + 20
15 libdispatch.dylib              0x1961b6cf4 _dispatch_continuation_pop + 448
16 libdispatch.dylib              0x1961b6384 _dispatch_async_redirect_invoke + 592
17 libdispatch.dylib              0x1961c4fe0 _dispatch_root_queue_drain + 388
18 libdispatch.dylib              0x1961c57d8 _dispatch_worker_thread2 + 112
19 libsystem_pthread.dylib        0x1e212a768 _pthread_wqthread + 216
20 libsystem_pthread.dylib        0x1e213174c start_wqthread + 8

That's line 93 crashing:

Crashed: NSOperationQueue 0x106c19630 (QOS: UNSPECIFIED)
0  libdispatch.dylib              0x1a97c40a0 _dispatch_queue_xref_dispose.cold.1 + 36
1  libdispatch.dylib              0x1a9794f84 _dispatch_queue_xref_dispose + 60
2  libdispatch.dylib              0x1a978e084 -[OS_dispatch_source _xref_dispose] + 28
3  SwiftPhoenixClient             0x1057f2634 HeartbeatTimer.deinit + 93 (HeartbeatTimer.swift:93)
4  SwiftPhoenixClient             0x1057f2670 HeartbeatTimer.__deallocating_deinit + 85 (HeartbeatTimer.swift:85)
5  libswiftCore.dylib             0x1ada81c98 _swift_release_dealloc + 40
6  libswiftCore.dylib             0x1ada82b90 bool swift::HeapObjectSideTableEntry::decrementStrong<(swift::PerformDeinit)1>(unsigned int) + 288
7  SwiftPhoenixClient             0x1058043c4 specialized Socket.onConnectionClosed(code:) + 598 (Socket.swift:598)
8  SwiftPhoenixClient             0x105802ad4 protocol witness for PhoenixTransportDelegate.onClose(code:) in conformance Socket + 4340787924 (<compiler-generated>:4340787924)
9  SwiftPhoenixClient             0x1057f492c URLSessionTransport.abnormalErrorReceived(_:) + 291 (PhoenixTransport.swift:291)
10 SwiftPhoenixClient             0x1057f42cc @objc URLSessionTransport.urlSession(_:task:didCompleteWithError:) + 4340728524 (<compiler-generated>:4340728524)
11 CFNetwork                      0x1aa3805c0 _CFNetworkHTTPConnectionCacheSetLimit + 152760
12 Foundation                     0x1aaf2bfa8 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 24
13 Foundation                     0x1aae19878 -[NSBlockOperation main] + 104
14 Foundation                     0x1aaf2e3d0 __NSOPERATION_IS_INVOKING_MAIN__ + 24
15 Foundation                     0x1aae19500 -[NSOperation start] + 808
16 Foundation                     0x1aaf2ee74 __NSOPERATIONQUEUE_IS_STARTING_AN_OPERATION__ + 24
17 Foundation                     0x1aaf2e900 __NSOQSchedule_f + 184
18 libdispatch.dylib              0x1a979d484 _dispatch_block_async_invoke2 + 148
19 libdispatch.dylib              0x1a978e81c _dispatch_client_callout + 20
20 libdispatch.dylib              0x1a9791cf4 _dispatch_continuation_pop + 448
21 libdispatch.dylib              0x1a9791384 _dispatch_async_redirect_invoke + 592
22 libdispatch.dylib              0x1a979ffe0 _dispatch_root_queue_drain + 388
23 libdispatch.dylib              0x1a97a07d8 _dispatch_worker_thread2 + 112
24 libsystem_pthread.dylib        0x1f577e768 _pthread_wqthread + 216
25 libsystem_pthread.dylib        0x1f578574c start_wqthread + 8
dsrees commented 3 years ago

Thanks for the issue. Seems like there may be a bug in the way the HeartbeatTimer interacts with it's DispatchSourceTimer. I'll try to look more into it.

Gamec commented 3 years ago

@dsrees any update on that?

dsrees commented 3 years ago

No sorry, I've been generally very busy lately. I am trying to set aside some time to take care of a couple of issues for this project. I will update here when I get around to it.

If you have time and want to try to figure it out, I would be happy to review a PR.

tankista commented 3 years ago

I have very similar timer class and the same crash. Problem is most likely with lazy property when accessed at the same time via multiple threads.

dsrees commented 3 years ago

Thanks for everyone for reporting on this Version 2.1.1 is out which fixes thread safety in HeartbeatTimer. Obviously testing this one is difficult so please let me know if you see something else pop up