Prevent callback being called twice when cancelling
This commit is contained in:
@@ -95,7 +95,7 @@ dependencies {
|
||||
implementation("com.takisoft.preferencex:preferencex-simplemenu:1.1.0")
|
||||
implementation("eu.chainfire:librootjava:1.3.0")
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-collections-immutable:0.3.2")
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.6")
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.8")
|
||||
testImplementation("junit:junit:4.13")
|
||||
androidTestImplementation("androidx.room:room-testing:$roomVersion")
|
||||
androidTestImplementation("androidx.test:runner:1.2.0")
|
||||
|
||||
@@ -475,36 +475,37 @@ class RootServer @JvmOverloads constructor(private val warnLogger: (String) -> U
|
||||
val commandJob = Job()
|
||||
cancellables[callback] = { commandJob.cancel() }
|
||||
defaultWorker.launch(commandJob) {
|
||||
try {
|
||||
val result = command.execute()
|
||||
withContext(callbackWorker + NonCancellable) { output.pushResult(callback, result) }
|
||||
val result = try {
|
||||
val result = command.execute();
|
||||
{ output.pushResult(callback, result) }
|
||||
} catch (e: Throwable) {
|
||||
withContext(callbackWorker + NonCancellable) { output.pushThrowable(callback, e) }
|
||||
{ output.pushThrowable(callback, e) }
|
||||
} finally {
|
||||
cancellables.remove(callback)
|
||||
}
|
||||
withContext(callbackWorker + NonCancellable) { result() }
|
||||
}
|
||||
}
|
||||
is RootCommandChannel<*> -> defaultWorker.launch {
|
||||
try {
|
||||
val result = try {
|
||||
coroutineScope {
|
||||
command.create(this).also {
|
||||
cancellables[callback] = { it.cancel() }
|
||||
}.consumeEach { result ->
|
||||
withContext(callbackWorker) { output.pushResult(callback, result) }
|
||||
}
|
||||
}
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
withContext(callbackWorker + NonCancellable) {
|
||||
};
|
||||
@Suppress("BlockingMethodInNonBlockingContext") {
|
||||
output.writeByte(CHANNEL_CONSUMED)
|
||||
output.writeLong(callback)
|
||||
output.flush()
|
||||
}
|
||||
} catch (e: Throwable) {
|
||||
withContext(callbackWorker + NonCancellable) { output.pushThrowable(callback, e) }
|
||||
{ output.pushThrowable(callback, e) }
|
||||
} finally {
|
||||
cancellables.remove(callback)
|
||||
}
|
||||
withContext(callbackWorker + NonCancellable) { result() }
|
||||
}
|
||||
is Shutdown -> break@loop
|
||||
else -> throw IllegalArgumentException("Unrecognized input: $command")
|
||||
|
||||
Reference in New Issue
Block a user