Skip to content

withContext should copy CopyableThreadContextElement #3414

Open
@dovchinnikov

Description

@dovchinnikov
val measurer = if (measure) MyCoroutineMeasurer() else null // CopyableThreadContextElement
runBlocking(Dispatchers.Default + CoroutineName("root") + measurer ?: EmptyCoroutineContext) {
  
  launch(CoroutineName("task 1") { ... } // ok
  
  async(CoroutineName("task 2") { ... } // ok
  
  // not copied but #updateThreadContext is still invoked
  withContext(CoroutineName("task 3")) { ... } 
}
measurer?.report()

I know that withContext {} is not equivalent to async {}.await(), but it does create a child coroutine (although UndispatchedCoroutine, but it still a separate instance) and introduces a CoroutineScope (it can also have children).

async(CoroutineName("task 3"), start = UNDISPATCHED) {}.await() kind of does what I want, but I also want the production code to look idiomatic since the measurer might or might not be present in the context.

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