KEMBAR78
async lazy val pattern causes infinite suspend · Issue #745 · Kotlin/kotlinx.coroutines · GitHub
Skip to content

async lazy val pattern causes infinite suspend #745

@antanas-arvasevicius

Description

@antanas-arvasevicius

Hi,
I've just noticed that using new structural coroutines, the code which uses async(start = CoroutineStart.LAZY) { } causes infinite suspend.

As I understand these async(start = CoroutineStart.LAZY) is now treated as a children of TestView object and new coroutine engine waits until these async completes. Shouldn't LAZY async coroutines be excluded from a wait list if they are not running?
Maybe there are more proper way to implement lazy async val?

test example:

class TestView(override val coroutineContext: CoroutineContext) : CoroutineScope {
    val title: Deferred<String> = async(start = CoroutineStart.LAZY) { "demo" }
    val description: Deferred<String> = async(start = CoroutineStart.LAZY) { "desc" }
}

class TestClass {
    suspend fun getView(id: Int): TestView = coroutineScope { TestView(coroutineContext = coroutineContext) }
}

runBlocking {
  val view = TestClass().getView(123)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions