Skip to content

fix: temporarily disable vpn toggle after toggling off #43

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 12, 2025
Merged
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
21 changes: 19 additions & 2 deletions 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(6)

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 {
!session.hasSession ||
waitCleanup ||
!session.hasSession ||
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
Loading