Skip to content

Commit f970cfc

Browse files
fix: improve wake & sleep handling (#74)
1 parent 3442516 commit f970cfc

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

Coder Desktop/VPN/PacketTunnelProvider.swift

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,17 @@ class PacketTunnelProvider: NEPacketTunnelProvider, @unchecked Sendable {
4848
options _: [String: NSObject]?, completionHandler: @escaping (Error?) -> Void
4949
) {
5050
logger.info("startTunnel called")
51+
guard manager == nil else {
52+
logger.error("startTunnel called with non-nil Manager")
53+
// If the tunnel is already running, then we can just mark as connected.
54+
completionHandler(nil)
55+
return
56+
}
5157
start(completionHandler)
5258
}
5359

5460
// called by `startTunnel` and on `wake`
5561
func start(_ completionHandler: @escaping (Error?) -> Void) {
56-
guard manager == nil else {
57-
logger.error("startTunnel called with non-nil Manager")
58-
completionHandler(makeNSError(suffix: "PTP", desc: "Already running"))
59-
return
60-
}
6162
guard let proto = protocolConfiguration as? NETunnelProviderProtocol,
6263
let baseAccessURL = proto.serverAddress
6364
else {
@@ -123,9 +124,11 @@ class PacketTunnelProvider: NEPacketTunnelProvider, @unchecked Sendable {
123124
logger.error("error stopping manager: \(error.description, privacy: .public)")
124125
}
125126
globalXPCListenerDelegate.vpnXPCInterface.manager = nil
127+
// Mark teardown as complete by setting manager to nil, and
128+
// calling the completion handler.
129+
self.manager = nil
126130
completionHandler()
127131
}
128-
self.manager = nil
129132
}
130133

131134
override func handleAppMessage(_ messageData: Data, completionHandler: ((Data?) -> Void)?) {
@@ -142,6 +145,13 @@ class PacketTunnelProvider: NEPacketTunnelProvider, @unchecked Sendable {
142145
}
143146

144147
override func wake() {
148+
// It's possible the tunnel is still starting up, if it is, wake should
149+
// be a no-op.
150+
guard !reasserting else { return }
151+
guard manager == nil else {
152+
logger.error("wake called with non-nil Manager")
153+
return
154+
}
145155
logger.debug("wake called")
146156
reasserting = true
147157
currentSettings = .init(tunnelRemoteAddress: "127.0.0.1")

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy