pusher / push-notifications-swift

Swift SDK for the Pusher Beams product:
https://www.pusher.com/beams
MIT License
34 stars 24 forks source link

crash when registering device tokens #111

Closed piemonte closed 5 years ago

piemonte commented 5 years ago

Hey Pusher,

I really value your folks products and am using your APNS service.

Recently, in release 1.2.1, I started seeing a crash after registering a device token. It seems to be happening when the preIIS operation queue is resumed after being suspended. Not sure why this would be happening.

Noticed an existing issue:

📎 https://github.com/pusher/push-notifications-swift/issues/71

Will that be applied to 1.2.1 other than the beta release?

Thread 38 Queue : com.pusher.pushnotifications.persistence.storage.operation.queue (serial)
#0  0x0000000109a8c0a0 in _dispatch_lane_resume ()
#1  0x0000000108067c00 in thunk for @escaping @callee_guaranteed () -> () ()
#2  0x0000000109a838a0 in _dispatch_call_block_and_release ()
#3  0x0000000109a84e38 in _dispatch_client_callout ()
#4  0x0000000109a8d128 in _dispatch_lane_serial_drain ()
#5  0x0000000109a8de3c in _dispatch_lane_invoke ()
#6  0x0000000109a97b30 in _dispatch_workloop_worker_thread ()
#7  0x00000002092c2b20 in _pthread_wqthread ()
#8  0x00000002092c8dd4 in start_wqthread ()
Enqueued from  (Thread 51) Queue :  (serial)
#0  0x0000000109ab9fe4 in _dispatch_introspection_queue_item_enqueue_hook ()
#1  0x0000000109a89d4c in dispatch_async ()
#2  0x00000001094f3008 in OS_dispatch_queue.async(group:qos:flags:execute:) ()
#3  0x000000010807200c in closure #1 in PushNotifications.registerDeviceToken(_:completion:) at /Pods/PushNotifications/Sources/PushNotifications.swift:236
#4  0x0000000108064d94 in closure #1 in NetworkService.register(url:deviceToken:instanceId:completion:) at /Pods/PushNotifications/Sources/NetworkService.swift:30
#5  0x0000000108067a38 in closure #1 in networkRequestWithExponentialBackoff #1 (numberOfAttempts:) in closure #1 in NetworkService.networkRequest(_:session:completion:) at /Pods/PushNotifications/Sources/NetworkService.swift:185
#6  0x00000001080685c0 in partial apply for closure #1 in networkRequestWithExponentialBackoff #1 (numberOfAttempts:) in closure #1 in NetworkService.networkRequest(_:session:completion:) ()
#7  0x000000010804e064 in thunk for @escaping @callee_guaranteed (@guaranteed Data?, @guaranteed NSURLResponse?, @guaranteed Error?) -> () ()
#8  0x0000000209ca107c in __75-[__NSURLSessionLocal taskForClass:request:uploadFile:bodyData:completion:]_block_invoke ()
#9  0x0000000209cb6528 in __49-[__NSCFLocalSessionTask _task_onqueue_didFinish]_block_invoke ()
#10 0x000000020a160da4 in __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ ()
#11 0x000000020a062158 in -[NSBlockOperation main] ()
#12 0x000000020a0615d8 in -[__NSOperationInternal _start:] ()
#13 0x000000020a162cdc in __NSOQSchedule_f ()
#14 0x0000000109a838a0 in _dispatch_call_block_and_release ()
#15 0x0000000109a84e38 in _dispatch_client_callout ()
#16 0x0000000109a8838c in _dispatch_continuation_pop ()
#17 0x0000000109a87758 in _dispatch_async_redirect_invoke ()
#18 0x0000000109a96110 in _dispatch_root_queue_drain ()
#19 0x0000000109a96b04 in _dispatch_worker_thread2 ()
#20 0x00000002092c2bc0 in _pthread_wqthread ()
#21 0x00000002092c8dd4 in start_wqthread ()
Enqueued from com.apple.NSURLSession-work (Thread 38) Queue : com.apple.NSURLSession-work (serial)
#0  0x0000000109ab9fe4 in _dispatch_introspection_queue_item_enqueue_hook ()
#1  0x0000000109a89d4c in dispatch_async ()
#2  0x000000020a160484 in __NSOQSchedule ()
#3  0x000000020a1619f4 in __addOperations ()
#4  0x0000000209c76b7c in -[__NSCFLocalSessionTask _task_onqueue_didFinish] ()
#5  0x0000000209c7ed00 in -[__NSCFLocalSessionTask _onqueue_submitConnectionWork:] ()
#6  0x0000000209c767b8 in -[__NSCFLocalSessionTask connection:didFinishLoadingWithError:] ()
#7  0x0000000209d3237c in __51-[__NSCFURLLocalSessionConnection _task_sendFinish]_block_invoke ()
#8  0x0000000209c825ac in -[__NSCFURLLocalSessionConnection withDelegateAndPendingCompletion:] ()
#9  0x0000000209c75c28 in -[__NSCFURLLocalSessionConnection _task_sendFinish] ()
#10 0x0000000109a93068 in _dispatch_block_async_invoke2 ()
#11 0x0000000109a84e38 in _dispatch_client_callout ()
#12 0x0000000109a8d128 in _dispatch_lane_serial_drain ()
#13 0x0000000109a8de3c in _dispatch_lane_invoke ()
#14 0x0000000109a97b30 in _dispatch_workloop_worker_thread ()
#15 0x00000002092c2b20 in _pthread_wqthread ()
#16 0x00000002092c8dd4 in start_wqthread ()
Enqueued from com.apple.NSURLSession-work (Thread 38) Queue : com.apple.NSURLSession-work (serial)
#0  0x0000000109ab9fe4 in _dispatch_introspection_queue_item_enqueue_hook ()
#1  0x0000000109a89d4c in dispatch_async ()
#2  0x0000000209c71dd4 in -[__NSCFURLSessionConnection withWorkQueueAsync:] ()
#3  0x0000000209c727f8 in -[__NSCFURLLocalSessionConnection afterDelegateWithTick:] ()
#4  0x0000000209d32520 in __50-[__NSCFURLLocalSessionConnection _tick_finishing]_block_invoke_2 ()
#5  0x0000000209c87314 in -[__NSCFLocalSessionTask _onqueue_willCacheResponse:withCompletion:] ()
#6  0x0000000209c7ed00 in -[__NSCFLocalSessionTask _onqueue_submitConnectionWork:] ()
#7  0x0000000209c81ff4 in -[__NSCFLocalSessionTask connection:willCacheResponse:responseCallback:] ()
#8  0x0000000209d324a4 in __50-[__NSCFURLLocalSessionConnection _tick_finishing]_block_invoke ()
#9  0x0000000209c825ac in -[__NSCFURLLocalSessionConnection withDelegateAndPendingCompletion:] ()
#10 0x0000000209c77ea8 in -[__NSCFURLLocalSessionConnection _tick_finishing] ()
#11 0x0000000109a93068 in _dispatch_block_async_invoke2 ()
#12 0x0000000109a84e38 in _dispatch_client_callout ()
#13 0x0000000109a8d128 in _dispatch_lane_serial_drain ()
#14 0x0000000109a8de3c in _dispatch_lane_invoke ()
#15 0x0000000109a97b30 in _dispatch_workloop_worker_thread ()
#16 0x00000002092c2b20 in _pthread_wqthread ()
#17 0x00000002092c8dd4 in start_wqthread ()
Enqueued from com.apple.NSURLSession-work (Thread 38) Queue : com.apple.NSURLSession-work (serial)
#0  0x0000000109ab9fe4 in _dispatch_introspection_queue_item_enqueue_hook ()
#1  0x0000000109a89d4c in dispatch_async ()
#2  0x0000000209c71dd4 in -[__NSCFURLSessionConnection withWorkQueueAsync:] ()
#3  0x0000000209c727f8 in -[__NSCFURLLocalSessionConnection afterDelegateWithTick:] ()
#4  0x0000000209d3211c in __48-[__NSCFURLLocalSessionConnection _tick_running]_block_invoke_2 ()
#5  0x0000000209c71a58 in -[__NSCFLocalSessionTask _task_onqueue_didReceiveDispatchData:completionHandler:] ()
#6  0x0000000209c7ed00 in -[__NSCFLocalSessionTask _onqueue_submitConnectionWork:] ()
#7  0x0000000209c716c8 in -[__NSCFLocalSessionTask connection:didReceiveData:completion:] ()
#8  0x0000000209d32054 in __48-[__NSCFURLLocalSessionConnection _tick_running]_block_invoke ()
#9  0x0000000209c825ac in -[__NSCFURLLocalSessionConnection withDelegateAndPendingCompletion:] ()
#10 0x0000000209c71424 in -[__NSCFURLLocalSessionConnection _tick_running] ()
#11 0x0000000109a93068 in _dispatch_block_async_invoke2 ()
#12 0x0000000109a84e38 in _dispatch_client_callout ()
#13 0x0000000109a8d128 in _dispatch_lane_serial_drain ()
#14 0x0000000109a8de3c in _dispatch_lane_invoke ()
#15 0x0000000109a97b30 in _dispatch_workloop_worker_thread ()
#16 0x00000002092c2b20 in _pthread_wqthread ()
#17 0x00000002092c8dd4 in start_wqthread ()
Enqueued from com.apple.NSURLSession-work (Thread 38) Queue : com.apple.NSURLSession-work (serial)
#0  0x0000000109ab9fe4 in _dispatch_introspection_queue_item_enqueue_hook ()
#1  0x0000000109a89d4c in dispatch_async ()
#2  0x0000000209c71dd4 in -[__NSCFURLSessionConnection withWorkQueueAsync:] ()
#3  0x0000000209c727f8 in -[__NSCFURLLocalSessionConnection afterDelegateWithTick:] ()
#4  0x0000000209d328c4 in __61-[__NSCFURLLocalSessionConnection _didReceiveResponse:sniff:]_block_invoke_2 ()
#5  0x0000000209cb784c in __67-[__NSCFLocalSessionTask connection:didReceiveResponse:completion:]_block_invoke_2 ()
#6  0x0000000209c75a18 in -[__NSCFLocalDataTask _onqueue_didReceiveResponse:completion:] ()
#7  0x0000000209cb77ec in __67-[__NSCFLocalSessionTask connection:didReceiveResponse:completion:]_block_invoke ()
#8  0x0000000209c7ed00 in -[__NSCFLocalSessionTask _onqueue_submitConnectionWork:] ()
#9  0x0000000209c755a8 in -[__NSCFLocalSessionTask connection:didReceiveResponse:completion:] ()
#10 0x0000000209d32800 in __61-[__NSCFURLLocalSessionConnection _didReceiveResponse:sniff:]_block_invoke ()
#11 0x0000000209c825ac in -[__NSCFURLLocalSessionConnection withDelegateAndPendingCompletion:] ()
#12 0x0000000209c74c34 in -[__NSCFURLLocalSessionConnection _didReceiveResponse:sniff:] ()
#13 0x0000000209c79ff0 in -[__NSCFURLLocalSessionConnection _tick_sniffNow] ()
#14 0x0000000209c825ac in -[__NSCFURLLocalSessionConnection withDelegateAndPendingCompletion:] ()
#15 0x0000000209c77604 in -[__NSCFURLLocalSessionConnection _didFinishWithError:] ()
#16 0x0000000209e0d718 in ___ZN19URLConnectionLoader24protocolDidFinishLoadingEv_block_invoke_5 ()
#17 0x0000000209d35a18 in ___ZN25SessionConnectionLoadable21withLoaderClientAsyncEU13block_pointerFvP21LoaderClientInterfaceE_block_invoke ()
#18 0x0000000109a93068 in _dispatch_block_async_invoke2 ()
#19 0x0000000109a84e38 in _dispatch_client_callout ()
#20 0x0000000109a8d128 in _dispatch_lane_serial_drain ()
#21 0x0000000109a8de3c in _dispatch_lane_invoke ()
#22 0x0000000109a97b30 in _dispatch_workloop_worker_thread ()
#23 0x00000002092c2b20 in _pthread_wqthread ()
#24 0x00000002092c8dd4 in start_wqthread ()
Enqueued from com.apple.NSURLSession-work (Thread 38) Queue : com.apple.NSURLSession-work (serial)
#0  0x0000000109ab9fe4 in _dispatch_introspection_queue_item_enqueue_hook ()
#1  0x0000000109a89d4c in dispatch_async ()
#2  0x0000000209c71dd4 in -[__NSCFURLSessionConnection withWorkQueueAsync:] ()
#3  0x0000000209d35044 in SessionConnectionLoadable::withLoaderClientAsync(void (LoaderClientInterface*) block_pointer) ()
#4  0x0000000209e0d424 in URLConnectionLoader::protocolDidFinishLoading() ()
#5  0x0000000209dfd078 in ___ZNK25URLConnectionInstanceData18withWorkQueueAsyncEU13block_pointerFvvE_block_invoke ()
#6  0x0000000109a838a0 in _dispatch_call_block_and_release ()
#7  0x0000000109a84e38 in _dispatch_client_callout ()
#8  0x0000000109a8d128 in _dispatch_lane_serial_drain ()
#9  0x0000000109a8de3c in _dispatch_lane_invoke ()
#10 0x0000000109a97b30 in _dispatch_workloop_worker_thread ()
#11 0x00000002092c2b20 in _pthread_wqthread ()
#12 0x00000002092c8dd4 in start_wqthread ()
Enqueued from com.apple.CFNetwork.Connection (Thread 51) Queue : com.apple.CFNetwork.Connection (serial)
#0  0x0000000109ab9fe4 in _dispatch_introspection_queue_item_enqueue_hook ()
#1  0x0000000109a89d4c in dispatch_async ()
#2  0x0000000209dfd040 in URLConnectionInstanceData::withWorkQueueAsync(void () block_pointer) const ()
#3  0x0000000209e115ec in non-virtual thunk to URLConnectionLoader_Classic::protocolDidFinishLoading() ()
#4  0x0000000209cf6494 in ___ZN11URLProtocol20sendDidFinishLoadingEv_block_invoke_2 ()
#5  0x0000000209cf5dac in URLProtocol::sendDidFinishLoading() ()
#6  0x0000000209db4ff8 in HTTPProtocol::endEncountered() ()
#7  0x0000000209db4680 in HTTPProtocol::handleStreamEvent(__CFHTTPMessage*, dispatch_data_s*, CFStreamError const*) ()
#8  0x0000000209ec5b30 in HTTPTransaction::_onqueue_invokeHandler() ()
#9  0x0000000209e54f64 in HTTPConnection::_onqueue_responseDataArrived(dispatch_data_s*, CFStreamError, bool) ()
#10 0x0000000209ebc380 in HTTPEngine::_readBodyFinish(dispatch_data_s*, CFStreamError, bool) ()
#11 0x0000000209ebcaf4 in HTTPEngine::_deliverBodyBytes(dispatch_data_s*, CFStreamError, bool) ()
#12 0x0000000209ebc740 in HTTPEngine::_getBodyIntelligently(void (dispatch_data_s*, CFStreamError, bool) block_pointer) ()
#13 0x0000000209eba478 in HTTPEngine::_readBodyStartNextRead() ()
#14 0x0000000209ebcb8c in HTTPEngine::_deliverBodyBytes(dispatch_data_s*, CFStreamError, bool) ()
#15 0x0000000209ebd454 in ___ZN10HTTPEngine21_getBodyIntelligentlyEU13block_pointerFvP15dispatch_data_s13CFStreamErrorbE_block_invoke ()
#16 0x0000000209ebd550 in ___ZN10HTTPEngine21_getBodyIntelligentlyEU13block_pointerFvP15dispatch_data_s13CFStreamErrorbE_block_invoke.56 ()
#17 0x0000000109a838a0 in _dispatch_call_block_and_release ()
#18 0x0000000109a84e38 in _dispatch_client_callout ()
#19 0x0000000109a8d128 in _dispatch_lane_serial_drain ()
#20 0x0000000109a8de78 in _dispatch_lane_invoke ()
#21 0x0000000109a8f304 in _dispatch_workloop_invoke2 ()
#22 0x0000000109a8ec84 in _dispatch_workloop_invoke ()
#23 0x0000000109a97b30 in _dispatch_workloop_worker_thread ()
#24 0x00000002092c2b20 in _pthread_wqthread ()
#25 0x00000002092c8dd4 in start_wqthread ()
Enqueued from com.apple.network.connections (Thread 51) Queue : com.apple.network.connections (serial)
#0  0x0000000109ab9fe4 in _dispatch_introspection_queue_item_enqueue_hook ()
#1  0x0000000109a89d4c in dispatch_async ()
#2  0x0000000209ebc990 in HTTPEngine::_getBodyIntelligently(void (dispatch_data_s*, CFStreamError, bool) block_pointer) ()
#3  0x0000000209eba478 in HTTPEngine::_readBodyStartNextRead() ()
#4  0x0000000209eb9f00 in HTTPEngine::_readHeadersFinish(CFStreamError) ()
#5  0x0000000209ebbad8 in HTTPEngine::_readHeadersDoParse(CFStreamError) ()
#6  0x0000000209ebd294 in ___ZN10HTTPEngine9_readBlobEmU13block_pointerFv13CFStreamErrorE_block_invoke ()
#7  0x0000000209e369a0 in TCPIOConnection::readCompleted(unsigned long, unsigned long, dispatch_data_s*, int, void (dispatch_data_s*, CFStreamError) block_pointer) ()
#8  0x0000000209ed2a9c in ConnectionProtocolReadInner(nw_protocol*, unsigned int, unsigned int, bool, long long, void (dispatch_data_s*, CFStreamError) block_pointer, void (nw_frame_array_s*, CFStreamError) block_pointer) ()
#9  0x0000000209ed25ec in ConnectionProtocolServiceReads(nw_protocol*) ()
#10 0x0000000209ed3510 in ConnectionProtocolInputAvailable(nw_protocol*, nw_protocol*) ()
#11 0x000000020a5683e8 in nw_boringssl_read ()
#12 0x000000020a569bfc in nw_protocol_boringssl_input_available ()
#13 0x000000020aa1470c in nw_protocol_tcp_wake_read ()
#14 0x000000020aa1a510 in nw_protocol_tcp_input_flush ()
#15 0x000000020a717f60 in nw_protocol_default_input_flush ()
#16 0x000000020a79da30 in nw_channel_add_input_frames ()
#17 0x000000020a79c970 in nw_channel_update_input_source ()
#18 0x000000020a79c1e8 in __nw_channel_create_block_invoke.21 ()
#19 0x0000000109a84e38 in _dispatch_client_callout ()
#20 0x0000000109a8838c in _dispatch_continuation_pop ()
#21 0x0000000109a9aeec in _dispatch_source_invoke2 ()
#22 0x0000000109a9a69c in _dispatch_source_invoke ()
#23 0x0000000109a8f304 in _dispatch_workloop_invoke2 ()
#24 0x0000000109a8ec84 in _dispatch_workloop_invoke ()
#25 0x0000000109a97b30 in _dispatch_workloop_worker_thread ()
#26 0x00000002092c2b20 in _pthread_wqthread ()
#27 0x00000002092c8dd4 in start_wqthread ()
lukabratos commented 5 years ago

Hi @piemonte

Thanks for reporting the issue, l'll try my best and help you to resolve it. You've mentioned that you've noticed this issue in version 1.2.1. Does it work in any other version?

Any additional information would be helpful so I can better understand your implementation and what could cause the issue.

Thanks!

piemonte commented 5 years ago

@lukabratos thanks so much!

I tried rolling back and using 1.2.0 as well but encountered the same issue. Simply following the example project structure but you can see the details here:

https://gist.github.com/piemonte/c01b78adc6bece37a072b0e8c342b85e

lukabratos commented 5 years ago

I have left a comment in your gist that you've shared.

piemonte commented 5 years ago

hey @lukabratos, thanks for the comments.

so we need to control when the user is prompted for this permission, which can't happen immediately after launch.

i'm assuming that start should only be called once registration has been confirmed to avoid the crash?

piemonte commented 5 years ago

sorry, i should rephrase that:

so never call start unless you immediately call registerForRemoteNotifications after otherwise the library will crash later in the future or be in a bad state?

lukabratos commented 5 years ago

You need to call registerForRemoteNotification eventually otherwise the SDK cannot get the device token that is needed for it to work.

Sometimes calling registerForRemoteNotifications just after the start method is not possible because you might have a certain UI workflow that at the end of it you would like to present the window asking user for permissions to enable remote notifications. Even it this case the SDK should not crash.

We handle a lot of things in the background in order to make the experience of the SDK as easy as we can. I'm not completely sure why this is happening in your case - it might be the way how you're controlling when the user is prompted for this permission.

We're improving some parts of the SDK at the moment so hopefully this issue will be resolved in the next release.

piemonte commented 5 years ago

@lukabratos thanks, i appreciate the help. will hang in and try the next update.

lukabratos commented 5 years ago

Hi @piemonte

Can you please update the SDK to the version 2.0.0 and let me know if you're still experiencing the same issue.

Thanks

piemonte commented 5 years ago

hey @lukabratos the crash appears to be fixed in 2.0.0

thanks so much for investigating and addressing the issue.