From 5e0235cf702c075c6eb8bc35494d6a9e109984f4 Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Fri, 21 Aug 2020 19:30:50 -0400 Subject: [PATCH] Fix test crash if node is removed This should fix almost all issues when the testing is in progress and the array is changed. However, there might be uncovered edge cases since the vmess array is in both process and accessed by multiple threads. --- .../src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt | 12 +++++++----- .../kotlin/com/v2ray/ang/util/AngConfigManager.kt | 12 +++++++++--- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt index 61565c80..ea3ac4b3 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt @@ -262,11 +262,13 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList serverPort = serverOutbound.getServerPort() ?: continue } testingJobs.add(GlobalScope.launch(Dispatchers.IO) { - configs.vmess[k].testResult = Utils.tcping(serverAddress, serverPort) - val myJob = coroutineContext[Job] - launch(Dispatchers.Main) { - testingJobs.remove(myJob) - adapter.updateSelectedItem(k) + configs.vmess.getOrNull(k)?.let { // check null in case array is modified during testing + it.testResult = Utils.tcping(serverAddress, serverPort) + val myJob = coroutineContext[Job] + launch(Dispatchers.Main) { + testingJobs.remove(myJob) + adapter.updateSelectedItem(k) + } } }) } diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/AngConfigManager.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/AngConfigManager.kt index 09358c0d..166b48af 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/AngConfigManager.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/AngConfigManager.kt @@ -795,10 +795,16 @@ object AngConfigManager { return 0 } val removedSelectedServer = - if (!TextUtils.isEmpty(subid) && configs.vmess.count() > 0 && configs.vmess[configs.index].subid.equals(subid)) - configs.vmess[configs.index] - else + if (!TextUtils.isEmpty(subid)) { + configs.vmess.getOrNull(configs.index)?.let { + if (it.subid == subid) { + return@let it + } + return@let null + } + } else { null + } removeServerViaSubid(subid) // var servers = server