I have an LaunchAgent using HockeyApp for crash reporting. Now I noticed that uncaught exception where not reported by HockeyApp, like they would have been in a normal macOS app.
For example:
- (void)upperCaseString:(NSString *)aString withReply:(void (^)(NSString *))reply {
NSArray *array = [NSArray array];
reply([array objectAtIndex:23]);
}
Never reaches NSUncaughtExceptionHandler, but the console logs:
<NSXPCConnection: 0x7fe97dc0f110> connection from pid 44573: Warning: Exception caught during invocation of received message, dropping incoming message and invalidating the connection.
The question is how to get unhandled exception reported with HockeyApp.
Problem:
XPC seems to have its own @try @catch block, which catches unhandled exceptions inside a method, logs the exception and then the calls
-[NSXPCConnection interruptionHandler].This issue is reported to Apple under rdar://48543049.
NOTE: These are not copy & past solutions, carefully evaluate your crash reporting framework. I link to implementation details of PLCrashReporter.
Solution A:
@try @catch block:
Discussion
HockeyApp uses PLCrashReporter for crash reporting. PLCrashReporter registers an
NSUncaughtExceptionHandler(code). So the above code will forward the exception to the PLCrashReporter exception handler and terminates (code) the XPC.Mattie suggest to @throw the exception again, to trigger the internal XPC @catch block and possible internal clean-up and logging. This is something to consider. Especially if you have a custom
interruptionHandleronNSXPCConnectionin the LaunchAgent/Server side of the connection!For now I side with not throwing it again, because my XPC is complete stateless and should be fine just crashing.
The downside to Solution A is that every method exposed via XPC requires this @try @catch block.
Solution B:
Use a
NSProxythat catches all unhandled exceptions asNSXPCConnection exportObject:Setup within the
NSXPCListenerlistener:All details of Solution A apply to Solution B.
Solution Z:
On macOS is possible to use the ExceptionHandling.framework, the problems with it are very well outlined in BITCrashExceptionApplication.h.
Discussion
It is never a good sign when a framework is not ported to iOS. Also note Matties comment: