push-notifications-swift
push-notifications-swift copied to clipboard
Crash at PushNotifications.swift:39
We had a few crashes being reported to Crashlytics with the following stack trace:
Crashed: com.pusher.beams.handleMessageQueue
0 redacted-app 0xdd2b74 closure #1 in PushNotifications.serverSyncHandler.getter + 39 (PushNotifications.swift:39)
1 redacted-app 0xde8e0c ServerSyncProcessHandler.processSetUserIdJob(userId:) + 306 (ServerSyncProcessHandler.swift:306)
2 redacted-app 0xde79e8 closure #1 in ServerSyncProcessHandler.processJob(_:) + 209 (ServerSyncProcessHandler.swift:209)
3 redacted-app 0xde7398 ServerSyncProcessHandler.processJob(_:) + 189 (ServerSyncProcessHandler.swift:189)
4 redacted-app 0xde9614 ServerSyncProcessHandler.handleMessage(serverSyncJob:) + 379 (ServerSyncProcessHandler.swift:379)
5 redacted-app 0xdeb3c8 partial apply for closure #1 in closure #1 in ServerSyncProcessHandler.init(instanceId:getTokenProvider:handleServerSyncEvent:) + 60 (ServerSyncProcessHandler.swift:60)
6 redacted-app 0xdd8b24 thunk for @escaping @callee_guaranteed () -> () + 6283756
7 libdispatch.dylib 0x2370 _dispatch_call_block_and_release + 32
8 libdispatch.dylib 0x40d0 _dispatch_client_callout + 20
9 libdispatch.dylib 0xb6d8 _dispatch_lane_serial_drain + 744
10 libdispatch.dylib 0xc1e0 _dispatch_lane_invoke + 380
11 libdispatch.dylib 0x17258 _dispatch_root_queue_drain_deferred_wlh + 288
12 libdispatch.dylib 0x16aa4 _dispatch_workloop_worker_thread + 540
13 libsystem_pthread.dylib 0x4c7c _pthread_wqthread + 288
14 libsystem_pthread.dylib 0x1488 start_wqthread + 8
The issue might be here:
private lazy var serverSyncHandler = ServerSyncProcessHandler.obtain(
instanceId: self.instanceId,
getTokenProvider: { return PushNotifications.shared.tokenProvider[self.instanceId] }, <---- HERE!
handleServerSyncEvent: { [weak self] event in
self?.serverSyncEventHandler.handleEvent(event: event)
}
)
instanceId can still be nil when serverSyncHandler is accessed, or maybe there is a race condition. On our side, we start the instance and set the user ID as expected. Any idea what could be happening and what we could do on our side to fix this? Thanks in advance.
Additional info
SDK: 4.0.0 iOS: 18.1.1
Hey @FilipeAVS , I guess you are unable to replicate this in your build?
To help us debug this, could you create a simple app that reflects your implementation and upload it so that we can try to re-produce the issue and also to make sure nothing stands out in the implementation? 🙏
Hey @aonemd! Exactly, I am unable to replicate the issue, and we have a very low count of users who experienced the same issue, so this might be a very specific edge case.
As for the implementation, hope you don't mind that I share here a snippet that represents how we are implementing it.
So from different parts of the app, we call the initialize function on our PushNotificationsManager class.
final class PushNotificationsManager {
static let shared = PushNotificationsManager()
private let userStore: UserStore
private let client = PushNotifications.shared
private init(userStore: UserStore = UserStore.shared) {
self.userStore = userStore
}
}
// MARK: - Public Methods
extension PushNotificationsManager {
func initialize() {
startInstance()
setUser()
}
}
// MARK: - Private Methods
private extension PushNotificationsManager {
func startInstance() {
client.start(instanceId: "...")
client.registerForRemoteNotifications()
}
func setUser() {
guard
let accessToken = userStore.accessToken,
let userId = userStore.userId
else { return }
let tokenProvider = BeamsTokenProvider(authURL: "...") { () -> AuthData in
let headers = ["Authorization": "Bearer \(accessToken)"]
let queryParams: [String: String] = [:]
return AuthData(headers: headers, queryParams: queryParams)
}
client.setUserId(userId, tokenProvider: tokenProvider) { error in
if let error {
print("Failed to authenticate to PushNotifications with error: \(error.localizedDescription)")
return
}
print("Successfully authenticated with PushNotifications")
}
}
}
With the above, in various contexts (like resuming the app, logging in, etc.) we call the initialize method.
PushNotificationsManager.shared.initialize()
This is the general implementation that we are using. Could it be that since we are calling the initialize method from different places, and some of them are very close to each other, we are hitting some race conditions? I appreciate your help @aonemd!
Hey @FilipeAVS thanks for posting your implementation! I have been trying to reproduce this with no success so far.
This might be related to https://github.com/pusher/push-notifications-swift/issues/190 btw.
Hey @aonemd, You can check this stacktrace, i'm also having same issue. Its happening when first time app installed.
Crashlytics - Stack trace
Fatal Exception: NSInvalidArgumentException 0 CoreFoundation 0x827cc __exceptionPreprocess 1 libobjc.A.dylib 0x172e4 objc_exception_throw 2 CoreFoundation 0x1888c8 +[NSObject(NSObject) copyDescription] 3 CoreFoundation 0x20b08 forwarding 4 CoreFoundation 0x20430 CF_forwarding_prep_0 5 libswiftCore.dylib 0xb1c50 Dictionary.Variant.setValue(:forKey:) 6 libswiftCore.dylib 0x59dd4 Dictionary.subscript.setter 7 BurnOn 0xfd05d4 PushNotifications.setUserId(:tokenProvider:completion:) + 116 (PushNotifications.swift:116) 8 BurnOn 0xfd4564 static PushNotificationsStatic.setUserId(:tokenProvider:completion:) + 104 (PushNotificationsStatic.swift:104) 9 BurnOn 0x1af8c closure #1 in PushNotificationManager.setUserId(_:accessToken:) + 77 (PushNotificationManager.swift:77) 10 BurnOn 0x170e9 partial apply for closure #1 in AuthService.authorizationController(controller:didCompleteWithAuthorization:) 11 BurnOn 0x4aab5 implicit closure #2 in ClientDetailViewModel.fetchClientDetails() + 226 (ClientDetailView.swift:226) 12 BurnOn 0x170e9 partial apply for closure #1 in AuthService.authorizationController(controller:didCompleteWithAuthorization:) 13 libswift_Concurrency.dylib 0x69e39 completeTaskWithClosure(swift::AsyncContext*, swift::SwiftError*)
com.apple.main-thread
0 CoreFoundation 0x2200 -[__NSCFConstantString copyWithZone:] + 1306
1 libswiftCore.dylib 0x220dc8 String.init(cocoaString:) + 28
2 Foundation 0x558fc static String.unconditionallyBridgeFromObjectiveC(:) + 688
3 SwiftUI 0x97124c @objc UIGraphicsView.shouldAnimateProperty(withKey:) + 40
4 UIKitCore 0x6582c -[UIView(UIKitManual) actionForLayer:forKey:] + 288
5 QuartzCore 0xabd8 -[CALayer actionForKey:] + 148
6 QuartzCore 0xaa4c CA::Layer::begin_change(CA::Transaction*, unsigned int, objc_object*, objc_object*&) + 204
7 QuartzCore 0x86e0 CA::Layer::set_position(CA::Vec2
Thread 0 libsystem_kernel.dylib 0x1410 __workq_kernreturn + 8 1 libsystem_pthread.dylib 0x4cc8 _pthread_wqthread + 364 2 libsystem_pthread.dylib 0x1488 start_wqthread + 8
com.google.firebase.crashlytics.MachExceptionServer 0 libsystem_kernel.dylib 0x1688 mach_msg2_trap + 8 1 libsystem_kernel.dylib 0x4d98 mach_msg2_internal + 80 2 libsystem_kernel.dylib 0x4cb0 mach_msg_overwrite + 424 3 libsystem_kernel.dylib 0x4afc mach_msg + 24 4 BurnOn 0x6b5b2c FIRCLSMachExceptionReadMessage + 192 (FIRCLSMachException.c:192) 5 BurnOn 0x6b5a64 FIRCLSMachExceptionServer + 168 (FIRCLSMachException.c:168) 6 libsystem_pthread.dylib 0x637c _pthread_start + 136 7 libsystem_pthread.dylib 0x1494 thread_start + 8
com.apple.network.connections 0 Network 0x35efc nw_endpoint_set_public_keys + 1704 1 Network 0x1d814 nw_context_copy_registered_endpoint_internal + 972 2 Network 0x1cfc8 nw_context_copy_registered_endpoint + 104 3 Network 0x1ce38 nw_endpoint_handler_initialize_association + 56 4 Network 0x33a4 nw_endpoint_handler_start + 1116 5 Network 0x1e4e5c nw_endpoint_resolver_start_next_child(NWConcrete_nw_endpoint_handler*) + 3272 6 libdispatch.dylib 0x2370 _dispatch_call_block_and_release + 32 7 libdispatch.dylib 0x40d0 _dispatch_client_callout + 20 8 libdispatch.dylib 0xd7f8 _dispatch_workloop_invoke + 2124 9 libdispatch.dylib 0x17258 _dispatch_root_queue_drain_deferred_wlh + 288 10 libdispatch.dylib 0x16aa4 _dispatch_workloop_worker_thread + 540 11 libsystem_pthread.dylib 0x4c7c _pthread_wqthread + 288 12 libsystem_pthread.dylib 0x1488 start_wqthread + 8
com.apple.uikit.eventfetch-thread 0 libsystem_kernel.dylib 0x1688 mach_msg2_trap + 8 1 libsystem_kernel.dylib 0x4d98 mach_msg2_internal + 80 2 libsystem_kernel.dylib 0x4cb0 mach_msg_overwrite + 424 3 libsystem_kernel.dylib 0x4afc mach_msg + 24 4 CoreFoundation 0x53a84 __CFRunLoopServiceMachPort + 160 5 CoreFoundation 0x53130 __CFRunLoopRun + 1212 6 CoreFoundation 0x52830 CFRunLoopRunSpecific + 588 7 Foundation 0xb7500 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 212 8 Foundation 0xb7350 -[NSRunLoop(NSRunLoop) runUntilDate:] + 64 9 UIKitCore 0x3e6358 -[UIEventFetcher threadMain] + 420 10 Foundation 0xc86c8 NSThread__start + 724 11 libsystem_pthread.dylib 0x637c _pthread_start + 136 12 libsystem_pthread.dylib 0x1494 thread_start + 8
com.apple.CFSocket.private 0 libsystem_kernel.dylib 0x9234 select$DARWIN_EXTSN + 8 1 libsystem_kernel.dylib 0x9234 __select + 8 2 CoreFoundation 0xbb95c __CFSocketManager + 704 3 libsystem_pthread.dylib 0x637c _pthread_start + 136 4 libsystem_pthread.dylib 0x1494 thread_start + 8
com.apple.NSURLConnectionLoader 0 libsystem_kernel.dylib 0x1688 mach_msg2_trap + 8 1 libsystem_kernel.dylib 0x4d98 mach_msg2_internal + 80 2 libsystem_kernel.dylib 0x4cb0 mach_msg_overwrite + 424 3 libsystem_kernel.dylib 0x4afc mach_msg + 24 4 CoreFoundation 0x53a84 __CFRunLoopServiceMachPort + 160 5 CoreFoundation 0x53130 __CFRunLoopRun + 1212 6 CoreFoundation 0x52830 CFRunLoopRunSpecific + 588 7 CFNetwork 0xfdee0 +[__CFN_CoreSchedulingSetRunnable _run:] + 416 8 Foundation 0xc86c8 NSThread__start + 724 9 libsystem_pthread.dylib 0x637c _pthread_start + 136 10 libsystem_pthread.dylib 0x1494 thread_start + 8
Thread 0 libsystem_kernel.dylib 0x1410 __workq_kernreturn + 8 1 libsystem_pthread.dylib 0x4cc8 _pthread_wqthread + 364 2 libsystem_pthread.dylib 0x1488 start_wqthread + 8
JavaScriptCore libpas scavenger 0 libsystem_kernel.dylib 0x6f90 __psynch_cvwait + 8 1 libsystem_pthread.dylib 0x3a50 _pthread_cond_wait + 1204 2 JavaScriptCore 0x15a6ca4 scavenger_thread_main + 1512 3 libsystem_pthread.dylib 0x637c _pthread_start + 136 4 libsystem_pthread.dylib 0x1494 thread_start + 8
Thread 0 libsystem_kernel.dylib 0x1410 __workq_kernreturn + 8 1 libsystem_pthread.dylib 0x4cc8 _pthread_wqthread + 364 2 libsystem_pthread.dylib 0x1488 start_wqthread + 8
com.google.fira.worker 0 libsystem_kernel.dylib 0x90f8 fsync + 8 1 libsqlite3.dylib 0x4c014 sqlite3_free_table + 46604 2 libsqlite3.dylib 0x550e8 sqlite3_randomness + 1652 3 libsqlite3.dylib 0x4bbd4 sqlite3_free_table + 45516 4 libsqlite3.dylib 0x3c518 sqlite3_value_text + 19792 5 libsqlite3.dylib 0x3c0e0 sqlite3_value_text + 18712 6 libsqlite3.dylib 0x1580c sqlite3_finalize + 1800 7 libsqlite3.dylib 0x36548 sqlite3_step + 45504 8 libsqlite3.dylib 0x2b748 sqlite3_step + 960 9 BurnOn 0x1091f2c -[APMSqliteStore executeSQLStatement:error:] + 9521152 10 BurnOn 0x1090878 -[APMSqliteStore upsertRecord:inTableWithName:error:] + 9515340 11 BurnOn 0x101366c -[APMDatabase upsertDataType:inTableWithName:columnName:columnValue:tableLimit:error:] + 9002816 12 BurnOn 0x100dbe4 -[APMDatabase updateUserAttribute:error:] + 8979640 13 BurnOn 0x1037218 -[APMMeasurement setUserPropertyInDatabaseOnWorkerQueue:] + 9149164 14 BurnOn 0x1036d28 -[APMMeasurement setUserAttributeOnWorkerQueue:] + 9147900 15 BurnOn 0x1036cd4 __35-[APMMeasurement setUserAttribute:]_block_invoke + 9147816 16 BurnOn 0x10888a4 __51-[APMScheduler scheduleOnWorkerQueueBlockID:block:]_block_invoke + 9482616 17 libdispatch.dylib 0x2370 _dispatch_call_block_and_release + 32 18 libdispatch.dylib 0x40d0 _dispatch_client_callout + 20 19 libdispatch.dylib 0xb6d8 _dispatch_lane_serial_drain + 744 20 libdispatch.dylib 0xc1e0 _dispatch_lane_invoke + 380 21 libdispatch.dylib 0x17258 _dispatch_root_queue_drain_deferred_wlh + 288 22 libdispatch.dylib 0x16aa4 _dispatch_workloop_worker_thread + 540 23 libsystem_pthread.dylib 0x4c7c _pthread_wqthread + 288 24 libsystem_pthread.dylib 0x1488 start_wqthread + 8
com.apple.SwiftUI.AsyncRenderer 0 libsystem_kernel.dylib 0x1688 mach_msg2_trap + 8 1 libsystem_kernel.dylib 0x4d98 mach_msg2_internal + 80 2 libsystem_kernel.dylib 0x4cb0 mach_msg_overwrite + 424 3 libsystem_kernel.dylib 0x4afc mach_msg + 24 4 CoreFoundation 0x53a84 __CFRunLoopServiceMachPort + 160 5 CoreFoundation 0x53130 __CFRunLoopRun + 1212 6 CoreFoundation 0x52830 CFRunLoopRunSpecific + 588 7 Foundation 0xb7500 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 212 8 Foundation 0xb73d4 -[NSRunLoop(NSRunLoop) run] + 64 9 SwiftUI 0x8f51f4 specialized static DisplayLink.asyncThread(arg:) + 792 10 SwiftUI 0x8f4558 @objc static DisplayLink.asyncThread(arg:) + 72 11 Foundation 0xc86c8 NSThread__start + 724 12 libsystem_pthread.dylib 0x637c _pthread_start + 136 13 libsystem_pthread.dylib 0x1494 thread_start + 8
Thread 0 libsystem_pthread.dylib 0x1480 start_wqthread + 9108194430
Thread 0 libsystem_kernel.dylib 0x1410 __workq_kernreturn + 8 1 libsystem_pthread.dylib 0x4cc8 _pthread_wqthread + 364 2 libsystem_pthread.dylib 0x1488 start_wqthread + 8
Crashed: com.google.firebase.crashlytics.ios.exception 0 BurnOn 0x69d5ec FIRCLSProcessGetThreadState + 197 (FIRCLSProcess.c:197) 1 BurnOn 0x69c938 FIRCLSProcessRecordThread + 423 (FIRCLSProcess.c:423) 2 BurnOn 0x69c6d0 FIRCLSProcessRecordAllThreads + 487 (FIRCLSProcess.c:487) 3 BurnOn 0x6b5098 FIRCLSHandler + 32 (FIRCLSHandler.m:32) 4 BurnOn 0x6b47fc __FIRCLSExceptionRecord_block_invoke + 238 (FIRCLSException.mm:238) 5 libdispatch.dylib 0x40d0 _dispatch_client_callout + 20 6 libdispatch.dylib 0x13750 dispatch_lane_barrier_sync_invoke_and_complete + 56 7 BurnOn 0x6b29b0 FIRCLSExceptionRecord + 226 (FIRCLSException.mm:226) 8 BurnOn 0x6b3e14 FIRCLSExceptionRecordNSException + 124 (FIRCLSException.mm:124) 9 BurnOn 0x6b4b6c FIRCLSCatchAndRecordActiveException(std::type_info*) + 401 (FIRCLSException.mm:401) 10 BurnOn 0x6b2500 FIRCLSTerminateHandler() + 456 (FIRCLSException.mm:456) 11 libc++abi.dylib 0x1287c std::__terminate(void ()()) + 16 12 libc++abi.dylib 0x15dfc __cxa_get_exception_ptr + 86 13 libc++abi.dylib 0x15da4 __cxxabiv1::failed_throw(__cxxabiv1::__cxa_exception) + 90 14 libobjc.A.dylib 0x1744c objc_exception_throw + 448 15 CoreFoundation 0x1888c8 +[NSObject(NSObject) copyDescription] + 362 16 CoreFoundation 0x20b08 forwarding + 1560 17 CoreFoundation 0x20430 CF_forwarding_prep_0 + 96 18 libswiftCore.dylib 0xb1c50 Dictionary.Variant.setValue(:forKey:) + 92 19 libswiftCore.dylib 0x59dd4 Dictionary.subscript.setter + 520 20 BurnOn 0xfd05d4 PushNotifications.setUserId(:tokenProvider:completion:) + 116 (PushNotifications.swift:116) 21 BurnOn 0xfd4564 static PushNotificationsStatic.setUserId(:tokenProvider:completion:) + 104 (PushNotificationsStatic.swift:104) 22 BurnOn 0x1af8c closure #1 in PushNotificationManager.setUserId(:accessToken:) + 77 (PushNotificationManager.swift:77) 23 libswift_Concurrency.dylib 0x61104 swift::runJobInEstablishedExecutorContext(swift::Job*) + 252 24 libswift_Concurrency.dylib 0x62514 swift_job_runImpl(swift::Job*, swift::SerialExecutorRef) + 144 25 libdispatch.dylib 0x15d8c _dispatch_root_queue_drain + 392 26 libdispatch.dylib 0x16590 _dispatch_worker_thread2 + 156 27 libsystem_pthread.dylib 0x4c40 _pthread_wqthread + 228 28 libsystem_pthread.dylib 0x1488 start_wqthread + 8