From 559a00e66e91e2284243259927f5bde8a18b52f2 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Mon, 3 Aug 2026 20:48:37 +0800 Subject: [PATCH] Fix cancel signaling in measure/ping services --- .../java/com/v2ray/ang/service/CoreTestService.kt | 1 + .../v2ray/ang/service/RealPingWorkerService.kt | 15 +++++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/service/CoreTestService.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/service/CoreTestService.kt index 85e25fe1..e300cf36 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/service/CoreTestService.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/service/CoreTestService.kt @@ -166,6 +166,7 @@ class CoreTestService : Service() { } private fun handleMeasureCancel() { + MessageHelper.sendMsg2UI(this, AppConfig.MSG_MEASURE_CONFIG_FINISH, "0") LogUtil.i(AppConfig.TAG, "CoreTestService received cancel message, cancelling ${activeWorkers.size} active workers") val snapshot = ArrayList(activeWorkers) snapshot.forEach { it.cancel() } diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/service/RealPingWorkerService.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/service/RealPingWorkerService.kt index e421aeda..d37346f1 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/service/RealPingWorkerService.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/service/RealPingWorkerService.kt @@ -15,6 +15,7 @@ import kotlinx.coroutines.CoroutineName import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.asCoroutineDispatcher +import kotlinx.coroutines.isActive import kotlinx.coroutines.joinAll import kotlinx.coroutines.launch import java.util.concurrent.Executors @@ -45,13 +46,17 @@ class RealPingWorkerService( runningCount.incrementAndGet() try { val result = if (onlyTcp) startTcping(guid) else startRealPing(guid) - onEvent(RealPingEvent.Result(guid, result)) + if (scope.isActive) { + onEvent(RealPingEvent.Result(guid, result)) + } } catch (_: Throwable) { // ignore } finally { val count = totalCount.decrementAndGet() val left = runningCount.decrementAndGet() - onEvent(RealPingEvent.Progress("$left / $count")) + if (scope.isActive) { + onEvent(RealPingEvent.Progress("$left / $count")) + } } } } @@ -59,9 +64,11 @@ class RealPingWorkerService( scope.launch { try { joinAll(*jobs.toTypedArray()) - onEvent(RealPingEvent.Finish("0")) + if (isActive) { + onEvent(RealPingEvent.Finish("0")) + } } catch (_: CancellationException) { - onEvent(RealPingEvent.Finish("-1")) + // If cancelled, don't send finish event to avoid confusion } finally { close() }