diff --git a/app/src/main/java/io/nekohasekai/sagernet/bg/proto/ProxyInstance.kt b/app/src/main/java/io/nekohasekai/sagernet/bg/proto/ProxyInstance.kt index 3a9439e9..7b70f5ca 100644 --- a/app/src/main/java/io/nekohasekai/sagernet/bg/proto/ProxyInstance.kt +++ b/app/src/main/java/io/nekohasekai/sagernet/bg/proto/ProxyInstance.kt @@ -36,6 +36,7 @@ import kotlinx.coroutines.runBlocking import libcore.Libcore import java.io.IOException import java.util.* +import java.util.concurrent.ConcurrentHashMap import kotlin.concurrent.timerTask class ProxyInstance(profile: ProxyEntity, val service: BaseService.Interface) : V2RayInstance( @@ -113,7 +114,7 @@ class ProxyInstance(profile: ProxyEntity, val service: BaseService.Interface) : } val updateTimer by lazy { Timer("Observatory Timer") } - val updateTasks by lazy { hashMapOf() } + val updateTasks by lazy { ConcurrentHashMap() } fun sendObservatoryResult(statusPb: ByteArray?) { if (statusPb == null || statusPb.isEmpty()) { @@ -146,7 +147,7 @@ class ProxyInstance(profile: ProxyEntity, val service: BaseService.Interface) : Logs.d("Send result for #$profileId ${profile.displayName()}") val groupId = profile.groupId - updateTasks.put(groupId, timerTask { + val task = timerTask { synchronized(this@ProxyInstance) { if (updateTasks[groupId] == this) { service.data.binder.broadcast { @@ -155,9 +156,9 @@ class ProxyInstance(profile: ProxyEntity, val service: BaseService.Interface) : updateTasks.remove(profile.groupId) } } - }.also { - updateTimer.schedule(it, 2333L) - })?.cancel() + } + updateTimer.schedule(task, 2000L) + updateTasks.put(groupId, task)?.cancel() } } else { Logs.d("Profile with id #$profileId not found") @@ -174,6 +175,7 @@ class ProxyInstance(profile: ProxyEntity, val service: BaseService.Interface) : persistStats() super.close() + if ((::updateTimer.getDelegate() as Lazy<*>).isInitialized()) updateTimer.cancel() if (::observatoryJob.isInitialized) observatoryJob.cancel() } diff --git a/buildSrc/src/main/kotlin/Helpers.kt b/buildSrc/src/main/kotlin/Helpers.kt index f30a355e..25b15b4d 100644 --- a/buildSrc/src/main/kotlin/Helpers.kt +++ b/buildSrc/src/main/kotlin/Helpers.kt @@ -496,6 +496,7 @@ fun Project.setupApp() { dependencies { add("implementation", kotlin("stdlib", "${rootProject.extra["kotlinVersion"]}")) + add("implementation", kotlin("reflect", "${rootProject.extra["kotlinVersion"]}")) add("implementation", project(":plugin:api")) add("testImplementation", "junit:junit:4.13.2") add("androidTestImplementation", "androidx.test.ext:junit:1.1.3") diff --git a/library/core b/library/core index 7a0d4fc6..d2dc6cc0 160000 --- a/library/core +++ b/library/core @@ -1 +1 @@ -Subproject commit 7a0d4fc6b7eeac93d945e803fa1aaf173723e2f3 +Subproject commit d2dc6cc068e08fd7a1411bd647a3a44680006d96