Fix cancel signaling in measure/ping services

This commit is contained in:
2dust committed 2026-08-03 20:48:37 +08:00
1 parent 31dfaeed6c
commit 559a00e66e
2 files changed
+12 -4

No files matched your search

@@ -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() }
@@ -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()
}