Fix race conditions for cancellables
This commit is contained in:
@@ -400,6 +400,7 @@ class RootServer {
|
|||||||
CoroutineScope(Dispatchers.Main.immediate + job)
|
CoroutineScope(Dispatchers.Main.immediate + job)
|
||||||
}
|
}
|
||||||
val callbackWorker = newSingleThreadContext("callbackWorker")
|
val callbackWorker = newSingleThreadContext("callbackWorker")
|
||||||
|
// access to cancellables shall be wrapped in defaultWorker
|
||||||
val cancellables = LongSparseArray<() -> Unit>()
|
val cancellables = LongSparseArray<() -> Unit>()
|
||||||
|
|
||||||
// thread safety: usage of output should be guarded by callbackWorker
|
// thread safety: usage of output should be guarded by callbackWorker
|
||||||
@@ -424,7 +425,7 @@ class RootServer {
|
|||||||
val callback = counter
|
val callback = counter
|
||||||
Logger.me.d("Received #$callback: $command")
|
Logger.me.d("Received #$callback: $command")
|
||||||
when (command) {
|
when (command) {
|
||||||
is CancelCommand -> cancellables[command.index]?.invoke()
|
is CancelCommand -> defaultWorker.launch { cancellables[command.index]?.invoke() }
|
||||||
is RootCommandOneWay -> defaultWorker.launch {
|
is RootCommandOneWay -> defaultWorker.launch {
|
||||||
try {
|
try {
|
||||||
command.execute()
|
command.execute()
|
||||||
@@ -434,8 +435,8 @@ class RootServer {
|
|||||||
}
|
}
|
||||||
is RootCommand<*> -> {
|
is RootCommand<*> -> {
|
||||||
val commandJob = Job()
|
val commandJob = Job()
|
||||||
cancellables.append(callback) { commandJob.cancel() }
|
|
||||||
defaultWorker.launch(commandJob) {
|
defaultWorker.launch(commandJob) {
|
||||||
|
cancellables.append(callback) { commandJob.cancel() }
|
||||||
val result = try {
|
val result = try {
|
||||||
val result = command.execute();
|
val result = command.execute();
|
||||||
{ output.pushResult(callback, result) }
|
{ output.pushResult(callback, result) }
|
||||||
|
|||||||
Reference in New Issue
Block a user