Prevent callback being called twice when cancelling

This commit is contained in:
Mygod
2020-07-28 03:31:37 +08:00
parent 680021a27b
commit 70ca91cd08
2 changed files with 11 additions and 10 deletions

View File

@@ -95,7 +95,7 @@ dependencies {
implementation("com.takisoft.preferencex:preferencex-simplemenu:1.1.0") implementation("com.takisoft.preferencex:preferencex-simplemenu:1.1.0")
implementation("eu.chainfire:librootjava:1.3.0") implementation("eu.chainfire:librootjava:1.3.0")
implementation("org.jetbrains.kotlinx:kotlinx-collections-immutable:0.3.2") 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") testImplementation("junit:junit:4.13")
androidTestImplementation("androidx.room:room-testing:$roomVersion") androidTestImplementation("androidx.room:room-testing:$roomVersion")
androidTestImplementation("androidx.test:runner:1.2.0") androidTestImplementation("androidx.test:runner:1.2.0")

View File

@@ -475,36 +475,37 @@ class RootServer @JvmOverloads constructor(private val warnLogger: (String) -> U
val commandJob = Job() val commandJob = Job()
cancellables[callback] = { commandJob.cancel() } cancellables[callback] = { commandJob.cancel() }
defaultWorker.launch(commandJob) { defaultWorker.launch(commandJob) {
try { val result = try {
val result = command.execute() val result = command.execute();
withContext(callbackWorker + NonCancellable) { output.pushResult(callback, result) } { output.pushResult(callback, result) }
} catch (e: Throwable) { } catch (e: Throwable) {
withContext(callbackWorker + NonCancellable) { output.pushThrowable(callback, e) } { output.pushThrowable(callback, e) }
} finally { } finally {
cancellables.remove(callback) cancellables.remove(callback)
} }
withContext(callbackWorker + NonCancellable) { result() }
} }
} }
is RootCommandChannel<*> -> defaultWorker.launch { is RootCommandChannel<*> -> defaultWorker.launch {
try { val result = try {
coroutineScope { coroutineScope {
command.create(this).also { command.create(this).also {
cancellables[callback] = { it.cancel() } cancellables[callback] = { it.cancel() }
}.consumeEach { result -> }.consumeEach { result ->
withContext(callbackWorker) { output.pushResult(callback, result) } withContext(callbackWorker) { output.pushResult(callback, result) }
} }
} };
@Suppress("BlockingMethodInNonBlockingContext") @Suppress("BlockingMethodInNonBlockingContext") {
withContext(callbackWorker + NonCancellable) {
output.writeByte(CHANNEL_CONSUMED) output.writeByte(CHANNEL_CONSUMED)
output.writeLong(callback) output.writeLong(callback)
output.flush() output.flush()
} }
} catch (e: Throwable) { } catch (e: Throwable) {
withContext(callbackWorker + NonCancellable) { output.pushThrowable(callback, e) } { output.pushThrowable(callback, e) }
} finally { } finally {
cancellables.remove(callback) cancellables.remove(callback)
} }
withContext(callbackWorker + NonCancellable) { result() }
} }
is Shutdown -> break@loop is Shutdown -> break@loop
else -> throw IllegalArgumentException("Unrecognized input: $command") else -> throw IllegalArgumentException("Unrecognized input: $command")