Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin: tailrec makes suspend fun never ends

I'm learning how to make coroutines work with Java libraries using CompletableFuture. Below is my code:

// x invokes y invokes z invokes Java client
suspend fun x(i: Int, client: FakeJavaClient): Int {

    fun z(k: Int): Int {
        println("z: $k")
        return client.query(k).get()
    }

    tailrec // with 'tailrec', the code never terminates
    suspend fun y(j: Int): Int {
        val ret = z(j)
        if (ret > 10) {
            return ret
        }

        return y(j + 1)
    }

    return y(i)
}

fun main()  {
    runBlocking {
        launch(Dispatchers.IO) {
            FakeJavaClient().use { x(0, it) }
        }
    }
    println("Done")
}

class FakeJavaClient : AutoCloseable {
    private val executor = Executors.newFixedThreadPool(10)

    fun query(i: Int): CompletableFuture<Int> {
        val f = CompletableFuture<Int>()
        executor.submit {
            Thread.sleep(1000)
            f.complete(i * 2)
        }
        return f
    }

    override fun close() {
        executor.shutdown()
        executor.awaitTermination(10, TimeUnit.SECONDS)
    }
}

If I add the tailrec modifier to function y, the code outputs like below and never ends:

z: 0
z: 0
z: 0
z: 0
z: 0
...

If I remove tailrec on y, the code behaves as my expectation

z: 0
z: 1
z: 2
z: 3
z: 4
z: 5
z: 6
Done

Could someone kindly help me understand what happens here ?

like image 482
kuang Avatar asked Nov 06 '22 09:11

kuang


1 Answers

This looks like a known issue: Wrong code generated for a local tailrec suspend function.

like image 92
Alexey Romanov Avatar answered Nov 17 '22 10:11

Alexey Romanov