Supported termination of a specific unresponsive embedded XPC service instance

We are evaluating a signed macOS launcher app with an embedded XPC service. The service would own a VZVirtualMachine. Before permitting VM startup, the launcher must bind the admitted job to the actual service instance and establish any required termination authority.

We need clarification on the following:

  1. Is there a public, supported mechanism for the containing app to terminate that specific service instance if it becomes unresponsive, including before its first application-level reply?
  2. If so, what API, signing, sandbox, entitlement, and authorization conditions apply? How can the termination target remain bound to that instance despite PID reuse or service regeneration?
  3. Is transfer of a task-control right from the service to the launcher, followed by task_terminate, supported for this purpose? If it requires the service to respond first, is there a supported alternative for a pre-reply hang?
  4. What supported observation confirms termination of that same process instance, separately from acceptance of a termination request?
  5. What lifecycle behavior applies if the containing launcher itself terminates unexpectedly?

We understand from threads 848542 and 850709 that Virtualization is expected to reclaim a guest after its owner exits, but provides neither a teardown timeline guarantee nor a successor-process guest-completion handle. We are not requesting such a guarantee or treating service exit as proof of completed VM reclamation.

Please identify applicable documentation and any platform-version restrictions.

Answered by DTS Engineer in 917858022
Written by shin0 in 858735021

1- Is there a public, supported mechanism for the containing app to terminate that specific service instance if it becomes unresponsive, including before its first application-level reply?

No.

There are workarounds you can apply here, but there’s no explicit mechanism where the container app can say “if XPC service ABC is running, stop it”.

Written by shin0 in 858735021

3- Is transfer of a task-control right from the service to the launcher, followed by task_terminate, supported for this purpose?

I wouldn’t do that. In general, passing around task control ports is a bad idea, and it also means you have to mess around with Mach APIs, which is also a bad idea. Moreover, I’ve no idea what task_terminate will do with the BSD aspects of the task.

If you’re going to go down that path then it’d be better to have the service returns its audit token to the app. The app can then kill the service with proc_signal_with_audittoken or proc_terminate_with_audittoken. See the discussion on this thread.

However, the XPC service still needs to respond to at least one message for this to work, and hence my negative answer to question 1. OTOH, the XPC service is embedded within your app and thus you have complete control over its code, allowing you to structure it in a way that it doesn’t do anything ‘risky’ until the app has checked in so as to obtain the audit token.

Written by shin0 in 858735021

4- What supported observation confirms termination of that same process instance … ?

Given that you already have an XPC connection to the service, the easiest way to observe termination is by watching for an interruption on that connection.

Written by shin0 in 858735021

5- What lifecycle behavior applies if the containing launcher itself terminates unexpectedly?

If an app terminates, regardless of how it terminates, the system will clean up any app-scoped XPC services that it started.

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

Accepted Answer
Written by shin0 in 858735021

1- Is there a public, supported mechanism for the containing app to terminate that specific service instance if it becomes unresponsive, including before its first application-level reply?

No.

There are workarounds you can apply here, but there’s no explicit mechanism where the container app can say “if XPC service ABC is running, stop it”.

Written by shin0 in 858735021

3- Is transfer of a task-control right from the service to the launcher, followed by task_terminate, supported for this purpose?

I wouldn’t do that. In general, passing around task control ports is a bad idea, and it also means you have to mess around with Mach APIs, which is also a bad idea. Moreover, I’ve no idea what task_terminate will do with the BSD aspects of the task.

If you’re going to go down that path then it’d be better to have the service returns its audit token to the app. The app can then kill the service with proc_signal_with_audittoken or proc_terminate_with_audittoken. See the discussion on this thread.

However, the XPC service still needs to respond to at least one message for this to work, and hence my negative answer to question 1. OTOH, the XPC service is embedded within your app and thus you have complete control over its code, allowing you to structure it in a way that it doesn’t do anything ‘risky’ until the app has checked in so as to obtain the audit token.

Written by shin0 in 858735021

4- What supported observation confirms termination of that same process instance … ?

Given that you already have an XPC connection to the service, the easiest way to observe termination is by watching for an interruption on that connection.

Written by shin0 in 858735021

5- What lifecycle behavior applies if the containing launcher itself terminates unexpectedly?

If an app terminates, regardless of how it terminates, the system will clean up any app-scoped XPC services that it started.

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

Thank you for the clear answer.
Marking as accepted.