Skip to content

fix: hold delegate reference to keep it from being deallocated #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Coder Desktop/Coder Desktop/SystemExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ protocol SystemExtensionAsyncRecorder: Sendable {
extension CoderVPNService: SystemExtensionAsyncRecorder {
func recordSystemExtensionState(_ state: SystemExtensionState) async {
sysExtnState = state
if state == .installed {
// system extension was successfully installed, so we don't need the delegate any more
systemExtnDelegate = nil
}
}

var extensionBundle: Bundle {
Expand Down Expand Up @@ -71,6 +75,7 @@ extension CoderVPNService: SystemExtensionAsyncRecorder {
queue: .main
)
let delegate = SystemExtensionDelegate(asyncDelegate: self)
systemExtnDelegate = delegate
request.delegate = delegate
OSSystemExtensionManager.shared.submitRequest(request)
logger.info("submitted SystemExtension request with bundleID: \(bundleID)")
Expand All @@ -87,6 +92,7 @@ class SystemExtensionDelegate<AsyncDelegate: SystemExtensionAsyncRecorder>:

init(asyncDelegate: AsyncDelegate) {
self.asyncDelegate = asyncDelegate
super.init()
logger.info("SystemExtensionDelegate initialized")
}

Expand Down
5 changes: 5 additions & 0 deletions Coder Desktop/Coder Desktop/VPNService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ final class CoderVPNService: NSObject, VPNService {

@Published var agents: [Agent] = []

// systemExtnDelegate holds a reference to the SystemExtensionDelegate so that it doesn't get
// garbage collected while the OSSystemExtensionRequest is in flight, since the OS framework
// only stores a weak reference to the delegate.
var systemExtnDelegate: SystemExtensionDelegate<CoderVPNService>?

override init() {
super.init()
installSystemExtension()
Expand Down