# Try..finally in buildSequence, finally not called in some situations

**URL:** https://discuss.kotlinlang.org/t/try-finally-in-buildsequence-finally-not-called-in-some-situations/4147
**Category:** Libraries
**Created:** [August 11, 2017, 1:50pm UTC](https://discuss.kotlinlang.org/t/try-finally-in-buildsequence-finally-not-called-in-some-situations/4147 "2017-08-11T13:50:44Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![mrange](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@mrange](https://discuss.kotlinlang.org/u/mrange)
#### Post date: [August 11, 2017, 1:50pm UTC](https://discuss.kotlinlang.org/t/try-finally-in-buildsequence-finally-not-called-in-some-situations/4147/1 "2017-08-11T13:50:44Z")

</div>

I have been experimenting with buildSequence and try…finally.

If I don’t iterate the full sequence it seems finally clause is not called.

Consider this

```auto
import kotlin.coroutines.experimental.buildSequence

fun test() = buildSequence {
    try {
        println("Yield 1")
        yield(1)
        println("Yield 2")
        yield(2)
        println("Yield 3")
        yield(3)
    } finally {
        println("Finally")
    }
}

fun main(argv: Array<String>) {
    for (v in test().take(2))
    {
        println("Received: $v")
    }
}

```

My expectation is that this program would print:

```auto
Yield 1
Received: 1
Yield 2
Received: 2
Finally

```

But it prints:

```auto
Yield 1
Received: 1
Yield 2
Received: 2

```

If I change to `take(100)` in order to iterate the full sequence finally seems to be invoked:

```auto
Yield 1
Received: 1
Yield 2
Received: 2
Yield 3
Received: 3
Finally

```

In a more realistic scenario this could mean that my code doesn’t call a database connection when I would expect it would.

Best Regards,  
orange

---

<div class="post-metadata">

### Author: ![jstuyts](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jstuyts/32/2141_2.png) [@jstuyts](https://discuss.kotlinlang.org/u/jstuyts)
#### Post date: [August 11, 2017, 2:14pm UTC](https://discuss.kotlinlang.org/t/try-finally-in-buildsequence-finally-not-called-in-some-situations/4147/2 "2017-08-11T14:14:21Z")

</div>

I can explain why it is not called, but I cannot offer a solution: Your sequence is frozen at `yield(2)`. Kotlin has no way of knowing it must exit at that point (i.e. you don’t (and can’t?) send a signal to the sequence telling it to terminate), and invoke the `finally` block.

---

<div class="post-metadata">

### Author: ![elizarov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/elizarov/32/2164_2.png) [@elizarov](https://discuss.kotlinlang.org/u/elizarov)
#### Post date: [August 14, 2017, 8:11am UTC](https://discuss.kotlinlang.org/t/try-finally-in-buildsequence-finally-not-called-in-some-situations/4147/3 "2017-08-14T08:11:29Z")

</div>

The `finally` is not invoked on _abandoned coroutines_ by design. More details about this particular design decision can be found in the corresponding design document: [coroutines-examples/kotlin-coroutines-informal.md at master · Kotlin/coroutines-examples · GitHub](https://github.com/Kotlin/kotlin-coroutines/blob/master/kotlin-coroutines-informal.md#resource-management-and-gc)

---

<div class="post-metadata">

### Author: ![mrange](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@mrange](https://discuss.kotlinlang.org/u/mrange)
#### Post date: [August 14, 2017, 9:14am UTC](https://discuss.kotlinlang.org/t/try-finally-in-buildsequence-finally-not-called-in-some-situations/4147/4 "2017-08-14T09:14:15Z")

</div>

I can understand abandoned coroutines should be avoided (like aborting threads) but a major feature of lazy sequences are that they can be “abandoned”.

This makes me wonder if `try..finally` should be forbidden in `buildSequence` to avoid confusion that may lead to file, mutex lock and connection leaks.

---

<div class="post-metadata">

### Author: ![Tim\_van\_der\_Leeuw.1](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/tim_van_der_leeuw.1/32/9155_2.png) [@Tim\_van\_der\_Leeuw.1](https://discuss.kotlinlang.org/u/Tim_van_der_Leeuw.1)
#### Post date: [August 14, 2017, 11:06am UTC](https://discuss.kotlinlang.org/t/try-finally-in-buildsequence-finally-not-called-in-some-situations/4147/5 "2017-08-14T11:06:54Z")

</div>

How would you forbid it?

The yield() call could happen from a closure and the code calling the closure could wrap that in a try-finally. I don’t think there’s a way to statically analyse this all at compile-time.

Is it possible to dynamically check this at runtime, in the generated byte-code?
