Reputation: 127
Does anyone know how to terminate or reset an XPC helper? According to the Apple documentation, launchd takes care of the XPC helper if it crashes. But there is no indication of what to do, if you need to interrupt or cancel a running helper.
For instance, if I have an XPC client that is rendering a 32-bit QuickTime movie, how do I get my 64-bit "parent" application to signal the XPC helper to cancel the job and clean up?
Also, What is the proper way for an XPC helper app to handle a parent that has "Quit"?
Currently, to terminate on the parent app's side, I am using (NSXPCConnection):
These seem to close off the connection. But I am not seeing any evidence that the helper app is paying attention.
Thanks in advance!
Upvotes: 6
Views: 1708
Reputation: 1607
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.
Upvotes: 2