I've noticed some unexpected behavior with a local variable in a function for Kotlin/Java. The following function was called concurrently several times, and sometimes the value of the variable temp is false instead of true.
What's more peculiar is that, I've verified that temp is set to true in every function invocation, but for some, it turns back to false in onTerminate.
My expectation was that for each function invocation, a new memory address will be used for the temp variable as long as the temp variable is not garbage collected. Since the variable is included in the callback/listener Completable, shouldn't it stay in memory, and so temp should always be true in the onTerminate section?
What is going on here?
fun getCompletable() {
    var temp = false
    return Completable.create {
        acquireLock()
        temp = true
        ...
    }.onTerminate {
        println(temp) // temp is sometimes true and sometimes false
    }
}