Skip to content

launch creates another child job #3426

@dovchinnikov

Description

@dovchinnikov
@OptIn(InternalCoroutinesApi::class)
@Suppress("DEPRECATION_ERROR", "INVISIBLE_MEMBER")
fun main() {
  runBlocking {
    launch(SupervisorJob()) {
      // failed child cancels parent, 
      // which turns out not to be a supervisor, 
      // but a regular StandaloneCoroutine
      async {
        throw RuntimeException("child failure")
      }
      // one can verify it 
      val thisJob = coroutineContext.job
      println(thisJob) // StandaloneCoroutine
      println((thisJob as JobSupport).parentHandle?.parent) // SupervisorJobImpl
    }.join()
  }
}

Basically, this is equivalent to

CoroutineScope(coroutineContext + SupervisorJob()).launch {
  ...
}

which, at least, is obvious.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions