recovering from XPC_ERROR_CONNECTION_INVALID to a priviledged helper service

I have a Qt app that uses the c xpc api to talk to an elevated launchd service that the app installs with SMJobBless. This all works fine. I'd like add the behavior that the next time the user launches the app it just starts communicating with the the service. The problem I'm actually trying to solve is determining if the service is already installed. I tried making a connection and sending a message, my thought was I'll get an invalid connection and if so, install the server and then try again. If I try this sequence I can't get the connection to function.


I've tried a few things to get this to work; canceling the first connection, releasing the first connection and making another, ignoring the first connection and creating another all to no avail. What does xpc need me to do to get it back into a state where it will connect after a fail with an invalid connection because the service doesn't exist yet?


Or is there a different way to determine it isn't there in the first place? Should I go spelunking the file system and look in /Library/PrivilegedHelperTools for my service executable and /Library/LaunchDaemons/ for the plist?


Cheers,

- James


PS I determined empirically that the error event block is executed in a thread created by the os but the reply block appears to alwats execute in the thread I send the message from. Did I miss the part in the docs where this is described?

I tried making a connection and sending a message, my thought was I'll get an invalid connection and if so, install the server and then try again.

That’s the approach I recommend. I’ve used it before and didn’t have a problem with it. Alas, it’s hard to say what’s going on in your specific case without knowing more details. I recommend that you try re-creating the problem with a minimal test project to see if it happens there.

PS I determined empirically that the error event block is executed in a thread created by the os but the reply block appears to alwats execute in the thread I send the message from. Did I miss the part in the docs where this is described?

Are you talking about the reply block you pass to

xpc_connection_send_message_with_reply
? If so, the reply block runs on the queue that you pass to it’s
replyq
.

IMPORTANT You have to be careful when looking at threads in an XPC world because XPC uses GCD’s thread pool, so there’s no strict relationship between threads and queues [1].

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

[1] Except for the main thread / main queue. However, even that breaks down if you run your process uses

dispatch_main
.

Hi Quinn,


Just knowing this retry should work helps a lot, thank you. Do I use the same connection I got back from xpc_connection_create_mach_service after a "connection invalid" event or do I need make a new one after install the service?


Cheers,

- James

Do I use the same connection I got back from

xpc_connection_create_mach_service
after a "connection invalid" event …

No. Once an

xpc_connection_t
object goes invalid, there’s no going back. You must create a new one in whatever way you created the original.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
recovering from XPC_ERROR_CONNECTION_INVALID to a priviledged helper service
 
 
Q