Skip to content

fix: improve wake & sleep handling #74

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 3 commits into from
Feb 24, 2025
Merged
Changes from 1 commit
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
17 changes: 11 additions & 6 deletions Coder Desktop/VPN/PacketTunnelProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ class PacketTunnelProvider: NEPacketTunnelProvider, @unchecked Sendable {
options _: [String: NSObject]?, completionHandler: @escaping (Error?) -> Void
) {
logger.info("startTunnel called")
guard manager == nil else {
logger.error("startTunnel called with non-nil Manager")
completionHandler(nil)
return
}
start(completionHandler)
}

// called by `startTunnel` and on `wake`
func start(_ completionHandler: @escaping (Error?) -> Void) {
guard manager == nil else {
logger.error("startTunnel called with non-nil Manager")
completionHandler(makeNSError(suffix: "PTP", desc: "Already running"))
return
}
guard let proto = protocolConfiguration as? NETunnelProviderProtocol,
let baseAccessURL = proto.serverAddress
else {
Expand Down Expand Up @@ -123,9 +123,9 @@ class PacketTunnelProvider: NEPacketTunnelProvider, @unchecked Sendable {
logger.error("error stopping manager: \(error.description, privacy: .public)")
}
globalXPCListenerDelegate.vpnXPCInterface.manager = nil
self.manager = nil
completionHandler()
}
self.manager = nil
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want self.manager being nil to be our code's perspective as to whether a stop operation has actually finished. Before, it was nil a little too early, though I don't think it was causing any issues.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind adding that as a code comment?


override func handleAppMessage(_ messageData: Data, completionHandler: ((Data?) -> Void)?) {
Expand All @@ -142,6 +142,11 @@ class PacketTunnelProvider: NEPacketTunnelProvider, @unchecked Sendable {
}

override func wake() {
guard !reasserting else { return }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

macOS does indeed wake a bunch during sleep to check the network. For us dogfooding, this means it might attempt to download a new dylib while the device sleeps, which the user won't be able to approve. In that case it'll get stuck in reasserting, in this case (and in any case, really) we don't want to try and start it up a second time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Would you also mind adding that as a code comment?

guard manager == nil else {
logger.error("wake called with non-nil Manager")
return
}
Comment on lines +151 to +154
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was previously in the start function called right after, but we should avoid resetting the network settings needlessly if we can.

logger.debug("wake called")
reasserting = true
currentSettings = .init(tunnelRemoteAddress: "127.0.0.1")
Expand Down
Loading