Is XPC reply guaranteed to be dispatched in a sole single thread?

I have this protocol method in the XPC server:

- (void)ping:(void (^)(NSString* reply))resultHandler;

Then I connect to the XPC server:

xpc.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(I_LoginHelperProtocol)]
_agent = [xpc remoteObjectProxyWithErrorHandler:^(NSError *error) {
  /
        MyLog(@"XPC error: %@", error);
        handler(error);
  }];

Then I call the ping method (or other methods) on the agent:

_agent ping:^(NSString *reply) {
        [_appLog log:@"LoginHelper Agent ping reply: %@ in thread %@", reply, [NSThread currentThread]];
    }];

My questions is - is it guaranteed that the reply handler is always dispatched in one single thread, or is it possible for XPC service to spawn a new thread to service the request?

Accepted Reply

You’re asking about threads when you should be asking about queues. NSXPCConnection replies are always delivered on a specific serial queue internal to the connection. There’s a comment in

<Foundation/NSXPCConnection.h>
that says as much:

Each NSXPCConnection instance has a private serial queue. This queue is used when sending messages to reply handlers, interruption handlers, and invalidation handlers.

Which thread runs blocks posted to that queue is not specified.

Share and Enjoy

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

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

Replies

You’re asking about threads when you should be asking about queues. NSXPCConnection replies are always delivered on a specific serial queue internal to the connection. There’s a comment in

<Foundation/NSXPCConnection.h>
that says as much:

Each NSXPCConnection instance has a private serial queue. This queue is used when sending messages to reply handlers, interruption handlers, and invalidation handlers.

Which thread runs blocks posted to that queue is not specified.

Share and Enjoy

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

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

Ah. That's exactly what I want. I only want to know if the reply handlers are called in serial mode so that I can determine some locks/synchronization could be omitted in my code.