xpc connection event handler not being called with XPC_ERROR_CONNECTION_INVALID

Hi,

I'm working on creating an xpc service, using the C APIs (gets launched from launchd via plist) and an application that wants to use this service.

Generally it works fine, communication is proper and as expected.

However, in case the service's plist has not been installed in /Library/LaunchDaemons, the client gets stuck! Neither the event handler (set with xpcconnectionseteventhandler) nor the reply block provided to xpcconnectionsendmessagewithreply get called.

If I use xpc
connectionsendmessagewithreplysync() - it never returns!

So how is a client to know that the service is not available in this case?

My assumption was that in such a case the event handler would be called with XPC
ERRORCONNECTIONINVALID.

Thanks in advance!
Devendra.
This is working for me. I created a new Mac app and wired up the following to a button:

Code Block
xpc_connection_t conn = xpc_connection_create_mach_service("com.example.apple-samplecode.Test657664", dispatch_get_main_queue(), XPC_CONNECTION_MACH_SERVICE_PRIVILEGED);
xpc_connection_set_event_handler(conn, ^(xpc_object_t object) {
#pragma unused(object)
if (object == XPC_ERROR_CONNECTION_INTERRUPTED) {
abort();
} else if (object == XPC_ERROR_CONNECTION_INVALID) {
abort();
} else {
abort();
}
});
xpc_connection_activate(conn);
const char * keys[1] = { "messageID" };
xpc_object_t values[1] = { xpc_string_create("varnishWaffles") };
xpc_object_t message = xpc_dictionary_create(keys, values, 1);
xpc_connection_send_message(conn, message);


The com.example.apple-samplecode.Test657664 service doesn’t exist, obviously. When I click the button I hit the second abort, indicating that I got XPC_ERROR_CONNECTION_INVALID, which is exactly what you’d expect when you try to message a non-existant service.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Thanks Quinn - I'm back to working on this - will give it a try now.

Will it make a difference if I'm using dispatchgetglobalqueue(QOSCLASSUSERINITIATED, 0) instead of the main queue?

Devendra.

will give it a try now.

Cool. Keep us posted.

Will it make a difference if I'm using
dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0) instead of
the main queue?

I doubt it.

Two random notes:
  • If you surround identifiers with backticks they’ll appear much nicer in your posts (in code font and without the underscores triggering italics).

  • I strongly recommend that you avoid using concurrent queues, including the standard global ones. While this is unlikely to be the cause of your current problem, concurrent queues are the source of much grief. For an explanation as to why, watch WWDC 2017 Session 706 Modernizing Grand Central Dispatch Usage.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
xpc connection event handler not being called with XPC_ERROR_CONNECTION_INVALID
 
 
Q