<@U0ZFBBUBU> with `kotlinx-coroutines-jdk8` you ca...
# coroutines
e
@groostav with
kotlinx-coroutines-jdk8
you can write something like:
Copy code
val coercedResult = future { 
    try { result.await() } 
    catch(ex: Throwable) { 
        Thread.handleUncaughtException(ex)
        emptyList() 
    } 
}
😀 1
g
elizarov: out of curiosity, if
coercedFuture
was a
var
, can you write
coercedFuture = future { coercedFuture.await() }
. It should right? The closure isn't going to capture changes in stack variables.
actually this can be trivially tested...
e
Kotlin closures caputure vars as mutable vars
Kotlin closures behave just like other
{ ... }
block. If you mutate something from there, it mutates.
g
yeah, so that makes the code I wrote a kind of descheduled deadlock. Can a CoroutineContext provide enough info for cycle detection?
e
can you elaborate on your problem?
g
Copy code
@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)
    }
causes deadlock
Im not thinking about a problem so much as a gotcha for newbies
im wondering if coroutines can programatically detect this behaviour, but I suppose that would only work for the
future
and
await
methods, not simply all coroutines generally
e
it is hard to detect programmatically
even Go is bad at detecting stuff like that
also `var`s are evil.
g
yeah i know, but its a supported language feature, somebody is going to write production code like I just sent you
and be suprised by a deadlock
e
there is plan
g
I'm also thinking a
CoroutineContext
check at the
future
and
await
boundaries should be sufficient
e
Coroutines are not like threads. They don't even have ids. But in debug mode they do, so we might try to write some kind of dead-lock detector based on that.
g
something like
Copy code
fun 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!")
}
e
This is an interesting idea. Could work!
There also an IDE proposal to warn on that kind of
var
-capturing code: https://youtrack.jetbrains.com/issue/KT-15514
g
yeah its funny, I was driven nuts once or twice by java's "variable must be effctively final", but I suppose this is simply the other side of that coin
e
Exactly