Your question doesn’t seem to ask for the proper handling of any crashes of the helper. Instead, you seem to simply need a way to properly tell the helper to terminate itself. If this is correct then please read on for a solution. At least if you still need one three years after asking…
You didn’t specify a language to use so please be aware my examples are written in Swift.
As part of your `NSXPCConnection* you had to define a protocol to be used for information sharing between app and helper. In this protocol you could add a simple function like this:
terminateHelper(withReply reply: (String) -> Void) {
reply("Terminating Helper.")
exit(0)
}
This function reports a message string back to the main app using the supplied closure and then terminates itself using the exit
system call.
From your main app, you would call this function like this:
if let helper = helperConnection.remoteObjectProxyWithErrorHandler({ (error) in
let e = error as NSError
print("Helper communication failed. Remote proxy error \(e.code): \(e.localizedDescription) \(e.localizedRecoverySuggestion ?? "---")")
}) as? HelperProtocol {
helper.terminateHelper(withReply: { (replyString) in
print(replyString)
})
}
Please not that launchd
will immediately restart the terminated helper app despite the fact that it hasn’t crashed but was gracefully terminated instead. This will, however, guarantee that the helper returns to an initialized state with all previous helper processing being stopped.
If you suspend or invalidate the way you put in your question then you only cancel the XPC connection. But neither suspending nor invalidating the connection will send a message of any kind to the helper. The helper itself will simply see that the connection is suspended or invalidated without knowing anything about the reason.
Hope this gives you at least an idea of how to proceed with your problem.