From a5898d4ad1f3c13ff2e41bed8d87946d80979210 Mon Sep 17 00:00:00 2001 From: Zane Schepke Date: Thu, 2 Jan 2025 17:10:32 -0500 Subject: [PATCH] fix: make sure ping shuts down --- .../service/tunnel/WireGuardTunnel.kt | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/tunnel/WireGuardTunnel.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/tunnel/WireGuardTunnel.kt index 0773fd7..67248a4 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/tunnel/WireGuardTunnel.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/tunnel/WireGuardTunnel.kt @@ -193,6 +193,7 @@ constructor( onTunnelStop(tunnelConfig) updateTunnelState(it, null) }.onFailure { + clearJobsAndStats() Timber.e(it) } } @@ -313,8 +314,7 @@ constructor( override fun cancelActiveTunnelJobs() { statsJob?.cancelWithMessage("Tunnel stats job cancelled") tunnelChangesJob?.cancelWithMessage("Tunnel changes job cancelled") - pingJob?.cancelWithMessage("Ping job cancelled") - networkJob?.cancelWithMessage("Network job cancelled") + cancelPingJobs() } override fun startActiveTunnelJobs() { @@ -326,6 +326,7 @@ constructor( } private fun startPingJobs() { + cancelPingJobs() pingJob = startPingJob() networkJob = startNetworkJob() } @@ -373,8 +374,7 @@ constructor( with(_vpnState.value.tunnelConfig) { if (this == null) return if (!isPingEnabled && pingJob?.isActive == true) { - pingJob?.cancelWithMessage("Ping job cancelled") - networkJob?.cancelWithMessage("Network job cancelled") + cancelPingJobs() return } restartPingJob() @@ -382,9 +382,13 @@ constructor( } private fun restartPingJob() { + cancelPingJobs() + startPingJobs() + } + + private fun cancelPingJobs() { pingJob?.cancelWithMessage("Ping job cancelled") networkJob?.cancelWithMessage("Network job cancelled") - startPingJobs() } private fun startTunnelConfigChangesJob() = applicationScope.launch(ioDispatcher) {