From 48ca2c1623d68c4037537c737d307d850e61901d Mon Sep 17 00:00:00 2001 From: Mygod Date: Thu, 23 Apr 2020 03:54:49 +0800 Subject: [PATCH] Prevent calling callback in synchronized --- .../mygod/vpnhotspot/net/monitor/DefaultNetworkMonitor.kt | 2 +- .../be/mygod/vpnhotspot/net/monitor/InterfaceMonitor.kt | 7 +++++-- .../be/mygod/vpnhotspot/net/monitor/IpNeighbourMonitor.kt | 7 ++++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/DefaultNetworkMonitor.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/DefaultNetworkMonitor.kt index 23afe83e..73ec86ae 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/DefaultNetworkMonitor.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/DefaultNetworkMonitor.kt @@ -85,7 +85,7 @@ object DefaultNetworkMonitor : UpstreamMonitor() { } catch (e: SecurityException) { // SecurityException would be thrown in requestNetwork on Android 6.0 thanks to Google's stupid bug if (Build.VERSION.SDK_INT != 23) throw e - callback.onFallback() + GlobalScope.launch { callback.onFallback() } return } registered = true diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/InterfaceMonitor.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/InterfaceMonitor.kt index 36bc0b9b..b8fe0acd 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/InterfaceMonitor.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/InterfaceMonitor.kt @@ -9,8 +9,11 @@ class InterfaceMonitor(val iface: String) : UpstreamMonitor() { val old = currentIface != null if (present == old) return currentIface = if (present) iface else null - if (present) { - val lp = currentLinkProperties ?: return@synchronized + if (present) Pair(iface, currentLinkProperties) else null + }.let { pair -> + if (pair != null) { + val (iface, lp) = pair + lp ?: return callbacks.forEach { it.onAvailable(iface, lp) } } else callbacks.forEach { it.onLost() } } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/IpNeighbourMonitor.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/IpNeighbourMonitor.kt index d3362c94..3537e417 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/IpNeighbourMonitor.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/IpNeighbourMonitor.kt @@ -15,14 +15,15 @@ class IpNeighbourMonitor private constructor() : IpMonitor() { var instance: IpNeighbourMonitor? = null fun registerCallback(callback: Callback) = synchronized(callbacks) { - if (!callbacks.add(callback)) return@synchronized + if (!callbacks.add(callback)) return@synchronized null var monitor = instance if (monitor == null) { monitor = IpNeighbourMonitor() instance = monitor monitor.flush() - } else callback.onIpNeighbourAvailable(monitor.neighbours.values) - } + null + } else monitor.neighbours.values + }?.let { callback.onIpNeighbourAvailable(it) } fun unregisterCallback(callback: Callback) = synchronized(callbacks) { if (!callbacks.remove(callback) || callbacks.isNotEmpty()) return@synchronized instance?.destroy()