Fix dispatcher not closed

This commit is contained in:
Mygod
2019-07-31 08:53:45 +08:00
parent bed11276b9
commit 1f2ccf83b0
3 changed files with 12 additions and 4 deletions

View File

@@ -46,7 +46,8 @@ class LocalOnlyHotspotService : IpNeighbourMonitoringService(), CoroutineScope {
/** /**
* Writes and critical reads to routingManager should be protected with this context. * 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 var routingManager: RoutingManager? = null
private val handler = Handler() private val handler = Handler()
@RequiresApi(28) @RequiresApi(28)
@@ -158,7 +159,10 @@ class LocalOnlyHotspotService : IpNeighbourMonitoringService(), CoroutineScope {
launch { launch {
routingManager?.destroy() routingManager?.destroy()
routingManager = null routingManager = null
if (exit) cancel() if (exit) {
cancel()
dispatcher.close()
}
} }
} }
} }

View File

@@ -149,7 +149,8 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
/** /**
* Writes and critical reads to routingManager should be protected with this context. * 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 routingManager: RoutingManager? = null
private var persistNextGroup = false private var persistNextGroup = false
@@ -419,6 +420,7 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
launch { // force clean to prevent leakage launch { // force clean to prevent leakage
cleanLocked() cleanLocked()
cancel() cancel()
dispatcher.close()
} }
if (Build.VERSION.SDK_INT < 29) @Suppress("DEPRECATION") { if (Build.VERSION.SDK_INT < 29) @Suppress("DEPRECATION") {
app.pref.unregisterOnSharedPreferenceChangeListener(this) app.pref.unregisterOnSharedPreferenceChangeListener(this)

View File

@@ -42,7 +42,8 @@ class TetheringService : IpNeighbourMonitoringService(), CoroutineScope {
/** /**
* Writes and critical reads to downstreams should be protected with this context. * 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 binder = Binder()
private val downstreams = ConcurrentHashMap<String, Downstream>() private val downstreams = ConcurrentHashMap<String, Downstream>()
private var receiverRegistered = false private var receiverRegistered = false
@@ -109,6 +110,7 @@ class TetheringService : IpNeighbourMonitoringService(), CoroutineScope {
downstreams.values.forEach { it.destroy() } // force clean to prevent leakage downstreams.values.forEach { it.destroy() } // force clean to prevent leakage
unregisterReceiver() unregisterReceiver()
cancel() cancel()
dispatcher.close()
} }
super.onDestroy() super.onDestroy()
} }