trilemma-dev / SecureXPC

A simple and secure XPC framework for Swift
MIT License
75 stars 15 forks source link

`XPC_ERROR_CONNECTION_INTERRUPTED` doesn't appear to actually behave as documented #70

Closed jakaplan closed 2 years ago

jakaplan commented 2 years ago

After releasing 0.3.0 of SecureXPC I started updating SwiftAuthorizationSample and have run into an unexpected issue:

  1. Over XPC the app asks the helper tool to uninstall itself
  2. As part of the helper tool uninstalling itself, the helper tool terminates
  3. This results in the client receiving XPC_ERROR_CONNECTION_INTERRUPTED. i. This is expected, Apple's documentation for interrupted says: "Will be delivered to the connection’s event handler if the remote service exited."
  4. Installer the helper tool again (under the hood via SMJobBless).
  5. The next send using the client results in a XPC_ERROR_CONNECTION_INVALID i. This is unexpected, Apple's documentation for interrupted also says: "The connection is still live even in this case, and resending a message will cause the service to be launched on-demand." ii. I can confirm the service is not launched on-demand.
  6. The subsequent send using the client results in the service being launched. i. This is not because of anything XPC is doing automatically, it's because when we received XPC_ERROR_CONNECTION_INVALID in step 4 we perform self.connection = nil which means we establish a new connection on the next send.
jakaplan commented 2 years ago

A simple way we can resolve this is to always set self.connection = nil when we receive XPC_ERROR_CONNECTION_INTERRUPTED.

@amomchilov thoughts on us doing this? It means we're explicitly going against Apple's documentation, but it appears their implementation is bugged and/or their documentation is incorrect.

amomchilov commented 2 years ago

i. This is unexpected, Apple's documentation for interrupted also says: "The connection is still live even in this case, and resending a message will cause the service to be launched on-demand." ii. I can confirm the service is not launched on-demand.

I don't think that's unexpected. They were talking about the process merely exiting. Here's it's existing, and deleting itself.

The subsequent send using the client results in the service being launched.

Now that's what I find to be unexpected. How did the service relaunch, if it deleted itself?

jakaplan commented 2 years ago

The subsequent send using the client results in the service being launched.

Now that's what I find to be unexpected. How did the service relaunch, if it deleted itself?

Because I'm dumb and wrote the repro steps wrong, missing a key step. I've revised it now to reflect what I was actually doing. This is now step 4:

  1. Installer the helper tool again (under the hood via SMJobBless).
amomchilov commented 2 years ago

Haahaha that makes quite a difference, doesn't it!

May I suggest renaming this issue to:

XPC_ERROR_CONNECTION_INTERRUPTED misbehaves when re-installing a service