Fix ConcurrentModification

This commit is contained in:
Mygod
2019-07-17 16:21:37 +08:00
parent f599e2fe3d
commit 7a4264e2df
2 changed files with 7 additions and 6 deletions

View File

@@ -330,7 +330,7 @@ class Routing(private val caller: Any, private val downstream: String) : IpNeigh
stop() stop()
Timber.i("Stopped routing for $downstream by $caller") Timber.i("Stopped routing for $downstream by $caller")
TrafficRecorder.update() // record stats before exiting to prevent stats losing TrafficRecorder.update() // record stats before exiting to prevent stats losing
clients.values.forEach { it.close() } synchronized(this) { clients.values.forEach { it.close() } }
currentDns?.transaction?.revert() currentDns?.transaction?.revert()
fallbackUpstream.subrouting?.transaction?.revert() fallbackUpstream.subrouting?.transaction?.revert()
upstream.subrouting?.transaction?.revert() upstream.subrouting?.transaction?.revert()

View File

@@ -6,14 +6,13 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.net.InetAddress import java.net.InetAddress
import java.util.* import java.util.*
import java.util.concurrent.ConcurrentHashMap
class IpNeighbourMonitor private constructor() : IpMonitor() { class IpNeighbourMonitor private constructor() : IpMonitor() {
companion object { companion object {
private val callbacks = Collections.newSetFromMap(ConcurrentHashMap<Callback, Boolean>()) private val callbacks = mutableSetOf<Callback>()
var instance: IpNeighbourMonitor? = null var instance: IpNeighbourMonitor? = null
fun registerCallback(callback: Callback) = synchronized(this) { fun registerCallback(callback: Callback) = synchronized(callbacks) {
if (!callbacks.add(callback)) return@synchronized if (!callbacks.add(callback)) return@synchronized
var monitor = instance var monitor = instance
if (monitor == null) { if (monitor == null) {
@@ -24,7 +23,7 @@ class IpNeighbourMonitor private constructor() : IpMonitor() {
callback.onIpNeighbourAvailable(synchronized(monitor.neighbours) { monitor.neighbours.values.toList() }) callback.onIpNeighbourAvailable(synchronized(monitor.neighbours) { monitor.neighbours.values.toList() })
} }
} }
fun unregisterCallback(callback: Callback) = synchronized(this) { fun unregisterCallback(callback: Callback) = synchronized(callbacks) {
if (!callbacks.remove(callback) || callbacks.isNotEmpty()) return@synchronized if (!callbacks.remove(callback) || callbacks.isNotEmpty()) return@synchronized
instance?.destroy() instance?.destroy()
instance = null instance = null
@@ -68,8 +67,10 @@ class IpNeighbourMonitor private constructor() : IpMonitor() {
updatePosted = false updatePosted = false
neighbours.values.toList() neighbours.values.toList()
} }
synchronized(callbacks) {
for (callback in callbacks) callback.onIpNeighbourAvailable(neighbours) for (callback in callbacks) callback.onIpNeighbourAvailable(neighbours)
} }
}
updatePosted = true updatePosted = true
} }
} }