From 81979aad38ec5403cbda864c874866cffd6ab7ef Mon Sep 17 00:00:00 2001 From: Mygod Date: Thu, 18 Jul 2019 14:10:39 +0800 Subject: [PATCH] Fix another concurrent modification --- .../be/mygod/vpnhotspot/net/monitor/IpLinkMonitor.kt | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/IpLinkMonitor.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/IpLinkMonitor.kt index 972b95a3..c921c1a3 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/IpLinkMonitor.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/IpLinkMonitor.kt @@ -10,7 +10,7 @@ class IpLinkMonitor private constructor() : IpMonitor() { private val callbacks = HashMap Unit>>() private var instance: IpLinkMonitor? = null - fun registerCallback(owner: Any, iface: String, callback: (Boolean) -> Unit) { + fun registerCallback(owner: Any, iface: String, callback: (Boolean) -> Unit) = synchronized(this) { check(callbacks.put(owner, Pair(iface, callback)) == null) var monitor = instance if (monitor == null) { @@ -19,8 +19,8 @@ class IpLinkMonitor private constructor() : IpMonitor() { } monitor.flush() } - fun unregisterCallback(owner: Any) { - if (callbacks.remove(owner) == null || callbacks.isNotEmpty()) return + fun unregisterCallback(owner: Any) = synchronized(this) { + if (callbacks.remove(owner) == null || callbacks.isNotEmpty()) return@synchronized instance?.destroy() instance = null } @@ -32,12 +32,14 @@ class IpLinkMonitor private constructor() : IpMonitor() { val match = parser.find(line) ?: return val iface = match.groupValues[2] val present = match.groupValues[1].isEmpty() - for ((target, callback) in callbacks.values) if (target == iface) callback(present) + synchronized(IpLinkMonitor) { + for ((target, callback) in callbacks.values) if (target == iface) callback(present) + } } override fun processLines(lines: Sequence) { val present = HashSet() for (it in lines) present.add((parser.find(it) ?: continue).groupValues[2]) - callbacks.values.forEach { (iface, callback) -> callback(present.contains(iface)) } + synchronized(IpLinkMonitor) { for ((iface, callback) in callbacks.values) callback(present.contains(iface)) } } }