diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/LocalOnlyHotspotService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/LocalOnlyHotspotService.kt index b5ea1898..53b2904a 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/LocalOnlyHotspotService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/LocalOnlyHotspotService.kt @@ -46,7 +46,8 @@ class LocalOnlyHotspotService : IpNeighbourMonitoringService(), CoroutineScope { /** * Writes and critical reads to routingManager should be protected with this context. */ - override val coroutineContext = newSingleThreadContext("LocalOnlyHotspotService") + Job() + private val dispatcher = newSingleThreadContext("LocalOnlyHotspotService") + override val coroutineContext = dispatcher + Job() private var routingManager: RoutingManager? = null private val handler = Handler() @RequiresApi(28) @@ -158,7 +159,10 @@ class LocalOnlyHotspotService : IpNeighbourMonitoringService(), CoroutineScope { launch { routingManager?.destroy() routingManager = null - if (exit) cancel() + if (exit) { + cancel() + dispatcher.close() + } } } } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt index 9536ae99..ce123249 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt @@ -149,7 +149,8 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene /** * Writes and critical reads to routingManager should be protected with this context. */ - override val coroutineContext = newSingleThreadContext("RepeaterService") + Job() + private val dispatcher = newSingleThreadContext("RepeaterService") + override val coroutineContext = dispatcher + Job() private var routingManager: RoutingManager? = null private var persistNextGroup = false @@ -419,6 +420,7 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene launch { // force clean to prevent leakage cleanLocked() cancel() + dispatcher.close() } if (Build.VERSION.SDK_INT < 29) @Suppress("DEPRECATION") { app.pref.unregisterOnSharedPreferenceChangeListener(this) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt index a476e6b2..00004964 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt @@ -42,7 +42,8 @@ class TetheringService : IpNeighbourMonitoringService(), CoroutineScope { /** * Writes and critical reads to downstreams should be protected with this context. */ - override val coroutineContext = newSingleThreadContext("TetheringService") + Job() + private val dispatcher = newSingleThreadContext("TetheringService") + override val coroutineContext = dispatcher + Job() private val binder = Binder() private val downstreams = ConcurrentHashMap() private var receiverRegistered = false @@ -109,6 +110,7 @@ class TetheringService : IpNeighbourMonitoringService(), CoroutineScope { downstreams.values.forEach { it.destroy() } // force clean to prevent leakage unregisterReceiver() cancel() + dispatcher.close() } super.onDestroy() }