Skip to content

Commit dfbd3d0

Browse files
committed
chore: gracefully shutdown app
1 parent 2f7f8f8 commit dfbd3d0

File tree

4 files changed

+70
-26
lines changed

4 files changed

+70
-26
lines changed

Coder Desktop/Coder Desktop/Coder_DesktopApp.swift

+8
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ class AppDelegate: NSObject, NSApplicationDelegate {
3737
}
3838
}
3939

40+
func applicationShouldTerminate(_: NSApplication) -> NSApplication.TerminateReply {
41+
Task {
42+
await vpn.stop()
43+
NSApp.reply(toApplicationShouldTerminate: true)
44+
}
45+
return .terminateLater
46+
}
47+
4048
func applicationShouldTerminateAfterLastWindowClosed(_: NSApplication) -> Bool {
4149
false
4250
}

Coder Desktop/Coder Desktop/Preview Content/PreviewVPN.swift

+32-13
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,46 @@ final class PreviewVPN: Coder_Desktop.VPNService {
2424
self.shouldFail = shouldFail
2525
}
2626

27+
var startTask: Task<Void, Never>?
2728
func start() async {
28-
state = .connecting
29-
do {
30-
try await Task.sleep(for: .seconds(10))
31-
} catch {
32-
state = .failed(.longTestError)
29+
if await startTask?.value != nil {
3330
return
3431
}
35-
state = shouldFail ? .failed(.longTestError) : .connected
32+
33+
startTask = Task {
34+
state = .connecting
35+
do {
36+
try await Task.sleep(for: .seconds(5))
37+
} catch {
38+
state = .failed(.longTestError)
39+
return
40+
}
41+
state = shouldFail ? .failed(.longTestError) : .connected
42+
}
43+
defer { startTask = nil }
44+
await startTask?.value
3645
}
3746

47+
var stopTask: Task<Void, Never>?
3848
func stop() async {
39-
guard state == .connected else { return }
40-
state = .disconnecting
41-
do {
42-
try await Task.sleep(for: .seconds(10))
43-
} catch {
44-
state = .failed(.longTestError)
49+
await startTask?.value
50+
guard state == .connected else { return}
51+
if await stopTask?.value != nil {
4552
return
4653
}
47-
state = .disabled
54+
55+
stopTask = Task {
56+
state = .disconnecting
57+
do {
58+
try await Task.sleep(for: .seconds(5))
59+
} catch {
60+
state = .failed(.longTestError)
61+
return
62+
}
63+
state = .disabled
64+
}
65+
defer { stopTask = nil }
66+
await stopTask?.value
4867
}
4968

5069
func configureTunnelProviderProtocol(proto _: NETunnelProviderProtocol?) {

Coder Desktop/Coder Desktop/VPNService.swift

+29-9
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,40 @@ final class CoderVPNService: NSObject, VPNService {
6363
installSystemExtension()
6464
}
6565

66+
var startTask: Task<Void, Never>?
6667
func start() async {
67-
tunnelState = .connecting
68-
await enableNetworkExtension()
68+
if await startTask?.value != nil {
69+
return
70+
}
71+
startTask = Task {
72+
tunnelState = .connecting
73+
await enableNetworkExtension()
6974

70-
// TODO: enable communication with the NetworkExtension to track state and agents. For
71-
// now, just pretend it worked...
72-
tunnelState = .connected
75+
// TODO: enable communication with the NetworkExtension to track state and agents. For
76+
// now, just pretend it worked...
77+
tunnelState = .connected
78+
}
79+
defer { startTask = nil }
80+
await startTask?.value
7381
}
7482

83+
var stopTask: Task<Void, Never>?
7584
func stop() async {
76-
tunnelState = .disconnecting
77-
await disableNetworkExtension()
78-
// TODO: determine when the NetworkExtension is completely disconnected
79-
tunnelState = .disabled
85+
// Wait for a start operation to finish first
86+
await startTask?.value
87+
guard state == .connected else { return }
88+
if await stopTask?.value != nil {
89+
return
90+
}
91+
stopTask = Task {
92+
tunnelState = .disconnecting
93+
await disableNetworkExtension()
94+
95+
// TODO: determine when the NetworkExtension is completely disconnected
96+
tunnelState = .disabled
97+
}
98+
defer { stopTask = nil }
99+
await stopTask?.value
80100
}
81101

82102
func configureTunnelProviderProtocol(proto: NETunnelProviderProtocol?) {

Coder Desktop/Coder Desktop/Views/VPNMenu.swift

+1-4
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@ struct VPNMenu<VPN: VPNService, S: Session>: View {
5959
}.buttonStyle(.plain)
6060
TrayDivider()
6161
Button {
62-
Task {
63-
await vpn.stop()
64-
NSApp.terminate(nil)
65-
}
62+
NSApp.terminate(nil)
6663
} label: {
6764
ButtonRowView {
6865
Text("Quit")

0 commit comments

Comments
 (0)