MainActor in Network Extension

I am adopting Swift Concurrency in my network extension app to use Swift 6 protections. In the UI app I ended up with most of the app marked as MainActor, so that pieces of my app can keep seamless access to each other and at the same time have thread safe access.

When it comes to my network extension, does it make sense to also mark most of the code as MainActor for the purposes of thread safety and seamless access of most classes to each other? I have doubts, because MainActor sounds like it should be a UI think, but network extension has no UI Of course any long or blocking operations would not be MainActor

Answered by DTS Engineer in 824585022

A network extension process will execute code scheduled on the main queue, and hence it is compatible with @MainActor. Two things to note here:

  • It’s not guaranteed to be running the main thread. So scheduling stuff on the main queue is fine, as is running stuff on the main actor. However, don’t schedule things on the main thread.

  • The calls you receive from NE — for example, when it invokes your NEPacketTunnelProvider.startTunnel(options:completionHandler:) method — are called on queue managed by NE, that is, not the main queue.

Share and Enjoy

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

A network extension process will execute code scheduled on the main queue, and hence it is compatible with @MainActor. Two things to note here:

  • It’s not guaranteed to be running the main thread. So scheduling stuff on the main queue is fine, as is running stuff on the main actor. However, don’t schedule things on the main thread.

  • The calls you receive from NE — for example, when it invokes your NEPacketTunnelProvider.startTunnel(options:completionHandler:) method — are called on queue managed by NE, that is, not the main queue.

Share and Enjoy

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

MainActor in Network Extension
 
 
Q