elizarov
05/18/2017, 9:28 PMkotlinx-coroutines-jdk8
you can write something like:
val coercedResult = future {
try { result.await() }
catch(ex: Throwable) {
Thread.handleUncaughtException(ex)
emptyList()
}
}
groostav
05/18/2017, 9:49 PMcoercedFuture
was a var
, can you write coercedFuture = future { coercedFuture.await() }
. It should right? The closure isn't going to capture changes in stack variables.groostav
05/18/2017, 9:49 PMelizarov
05/18/2017, 9:55 PMelizarov
05/18/2017, 9:56 PM{ ... }
block. If you mutate something from there, it mutates.groostav
05/18/2017, 9:56 PMelizarov
05/18/2017, 9:57 PMgroostav
05/18/2017, 9:58 PM@Test fun `when using mutable stack future var should properly snapshot in closure`(){
//setup
val initial = CompletableFuture<Int>()
var ref = initial
ref = future {
ref.await() !!!! //this code deadlocks!
}
//act
initial.complete(42)
val result = ref.get()
//assert
assertThat(result).isEqualTo(42)
}
groostav
05/18/2017, 9:58 PMgroostav
05/18/2017, 9:58 PMgroostav
05/18/2017, 10:01 PMfuture
and await
methods, not simply all coroutines generallyelizarov
05/18/2017, 10:03 PMelizarov
05/18/2017, 10:03 PMelizarov
05/18/2017, 10:04 PMgroostav
05/18/2017, 10:04 PMgroostav
05/18/2017, 10:04 PMelizarov
05/18/2017, 10:04 PMgroostav
05/18/2017, 10:04 PMCoroutineContext
check at the future
and await
boundaries should be sufficientelizarov
05/18/2017, 10:06 PMgroostav
05/18/2017, 10:06 PMfun future(ctx: CoroutineContext){
val newContext = newCoroutineContext(CommonPool + context + BlacklistFuture(future))
//...
}
fun Future.await() {
if(this in currentContext.blacklistedFutures.map { it.future) throw IllegalStateException("cyclic call to future!")
}
elizarov
05/18/2017, 10:07 PMelizarov
05/18/2017, 10:07 PMvar
-capturing code: https://youtrack.jetbrains.com/issue/KT-15514groostav
05/18/2017, 10:08 PMelizarov
05/18/2017, 10:11 PM