diff --git a/swift/apple/FirezoneNetworkExtension/Adapter.swift b/swift/apple/FirezoneNetworkExtension/Adapter.swift index 45cf357ee..cc905f394 100644 --- a/swift/apple/FirezoneNetworkExtension/Adapter.swift +++ b/swift/apple/FirezoneNetworkExtension/Adapter.swift @@ -416,15 +416,17 @@ extension Adapter: CallbackHandlerDelegate { } func onDisconnect(error: DisconnectError) { - // Immediately invalidate our session pointer to prevent workQueue items from trying to use it. - // Assigning to `nil` will invoke `Drop` on the Rust side. - session = nil - // Since connlib has already shutdown by this point, we queue this callback // to ensure that we can clean up even if connlib exits before we are done. workQueue.async { [weak self] in guard let self = self else { return } + // Immediately invalidate our session pointer to prevent workQueue items from trying to use it. + // Assigning to `nil` will invoke `Drop` on the Rust side. + // This must happen asynchronously and not as part of the callback to allow Rust to break + // cyclic dependencies between the runtime and the task that is executing the callback. + self.session = nil + // If auth expired/is invalid, delete stored token and save the reason why so the GUI can act upon it. if error.isAuthenticationError() { do {