XPC connection hangs; is there a way to do a timeout?

This specifically seems to happen when my network extension is spawned by launchd, but has not actually been connected. (That is my conclusion based on the evidence: ps shows the process existing, but the logs show that main() hasn't run yet, let alone the proxy provider's init(). Without being able to debug launchd, I would say the situation is that launchd has said "yes this XPC port exists, when you try to connect to it I will send it on over to the extension, and thereafter you two will happily communicate directly," but the process hasn't gotten around to accepting XPC connections.) This seems to be most likely to happen when the extension crashes, or I kill -9 it to see how it handles respawning.

I have a little CLI tool that talks (over XPC, of course) to the extension, mainly to say "Hi are you alive?" If the extension is not loaded, the XPC connection fails, as expected, and my tool says "NOT YET." If it is loaded and running, the XPC connection succeeds, as expected, and I get back a response of true.

So my first question is: is this expected behaviour?

My second question is: is there a way to do an XPC connection with a timeout?

To answer your second question first, XPC doesn’t support timeouts. It’s focus is IPC and timeouts don’t make a lot of sense in that context.

If you must implement a timeout, just run your own timer. This is one advantage of having an async API.


With regards your first question, it’s hard to say what’s going on there. launchd should handle the case where a job crashes or is killed. If there is still IPC demand for the job, launchd will simply relaunch it. If you can find a way to reliably reproduce this, I’d be happy to dig into it further.

Are you using NSXPCConnection? Or the C API?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I'm using NSXPCConnection. (The extension is in fact written in Swift, since I figured that'd be better, although weirdly I still sometimes get a segfault.)

I'm using NSXPCConnection.

In that case I recommend that you use -remoteObjectProxyWithErrorHandler:. When you call an method in the XPC protocol on such a proxy that has a reply block, it guarantees to eventually call either that reply block or the error handler. Keep that in mind when you design your timeout.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

XPC connection hangs; is there a way to do a timeout?
 
 
Q