Skip to content

Commit

Permalink
fix: temporarily disable vpn toggle after toggling off
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanndickson committed Feb 12, 2025
1 parent ddf21a7 commit febf414
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion Coder Desktop/Coder Desktop/Views/VPNMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ struct VPNMenu<VPN: VPNService, S: Session>: View {
@EnvironmentObject var session: S
@Environment(\.openSettings) private var openSettings

// There appears to be a race between the VPN service reporting itself as disconnected,
// and the system extension process exiting. When the VPN is toggled off and on quickly,
// an error is shown: "The VPN session failed because an internal error occurred".
// This forces the user to wait a few seconds before they can toggle the VPN back on.
@State private var waitCleanup = false
private var waitCleanupDuration: Duration = .seconds(4)

Check warning on line 13 in Coder Desktop/Coder Desktop/Views/VPNMenu.swift

View workflow job for this annotation

GitHub Actions / test

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)

Check failure on line 13 in Coder Desktop/Coder Desktop/Views/VPNMenu.swift

View workflow job for this annotation

GitHub Actions / lint

Lines should not have trailing whitespace (trailing_whitespace)

Check warning on line 13 in Coder Desktop/Coder Desktop/Views/VPNMenu.swift

View workflow job for this annotation

GitHub Actions / fmt

Remove trailing space at end of a line. (trailingSpace)

let inspection = Inspection<Self>()

var body: some View {
Expand All @@ -16,7 +23,7 @@ struct VPNMenu<VPN: VPNService, S: Session>: View {
Toggle(isOn: Binding(
get: { vpn.state == .connected || vpn.state == .connecting },
set: { isOn in Task {
if isOn { await vpn.start() } else { await vpn.stop() }
if isOn { await vpn.start() } else { await stop() }
}
}
)) {
Expand Down Expand Up @@ -86,11 +93,21 @@ struct VPNMenu<VPN: VPNService, S: Session>: View {
}

private var vpnDisabled: Bool {
waitCleanup ||
!session.hasSession ||

Check warning on line 97 in Coder Desktop/Coder Desktop/Views/VPNMenu.swift

View workflow job for this annotation

GitHub Actions / fmt

Indent code in accordance with the scope level. (indent)
vpn.state == .connecting ||
vpn.state == .disconnecting ||
vpn.state == .failed(.systemExtensionError(.needsUserApproval))
}

private func stop() async {
await vpn.stop()
waitCleanup = true
Task {
try await Task.sleep(for: waitCleanupDuration)
waitCleanup = false
}
}
}

func openSystemExtensionSettings() {
Expand Down

0 comments on commit febf414

Please sign in to comment.