kotlinx.coroutines icon indicating copy to clipboard operation
kotlinx.coroutines copied to clipboard

Support `CoroutineName`, which is not propagated to child coroutines

Open dovchinnikov opened this issue 1 year ago • 0 comments
trafficstars

Use case

In IJ we log coroutine dumps, here's an example subtree:

- "com.intellij.openapi.fileEditor.impl.PsiAwareFileEditorManagerImpl":StandaloneCoroutine{Active}, state: SUSPENDED [ComponentManager(ProjectImpl@1365006477), Dispatchers.Default]
	at kotlinx.coroutines.flow.FlowKt__ChannelsKt.emitAllImpl$FlowKt__ChannelsKt(Channels.kt:36)
	at kotlinx.coroutines.flow.internal.ChannelFlow$collect$2.invokeSuspend(ChannelFlow.kt:123)
	at kotlinx.coroutines.flow.FlowKt__ShareKt$launchSharing$1.invokeSuspend(Share.kt:214)
	- "com.intellij.openapi.fileEditor.impl.PsiAwareFileEditorManagerImpl":ProducerCoroutine{Active}, state: SUSPENDED [ComponentManager(ProjectImpl@1365006477), Dispatchers.Default]
		at kotlinx.coroutines.flow.FlowKt__ChannelsKt.emitAllImpl$FlowKt__ChannelsKt(Channels.kt:36)
		at kotlinx.coroutines.flow.internal.ChannelFlow$collect$2.invokeSuspend(ChannelFlow.kt:123)
		at kotlinx.coroutines.flow.internal.ChannelFlowTransformLatest$flowCollect$3.invokeSuspend(Merge.kt:27)
		at kotlinx.coroutines.flow.internal.ChannelFlow$collectToFun$1.invokeSuspend(ChannelFlow.kt:60)
		- "com.intellij.openapi.fileEditor.impl.PsiAwareFileEditorManagerImpl":ProducerCoroutine{Active}, state: SUSPENDED [ComponentManager(ProjectImpl@1365006477), Dispatchers.Default]
			at kotlinx.coroutines.flow.SharedFlowImpl.collect$suspendImpl(SharedFlow.kt:379)
			at kotlinx.coroutines.flow.internal.ChannelFlowTransformLatest$flowCollect$3.invokeSuspend(Merge.kt:27)
			at kotlinx.coroutines.flow.internal.ChannelFlow$collectToFun$1.invokeSuspend(ChannelFlow.kt:60)
			- "com.intellij.openapi.fileEditor.impl.PsiAwareFileEditorManagerImpl":StandaloneCoroutine{Active}, state: SUSPENDED [ComponentManager(ProjectImpl@1365006477), Dispatchers.Default]
				at kotlinx.coroutines.flow.StateFlowImpl.collect(StateFlow.kt:401)
				at com.intellij.openapi.fileEditor.impl.FileEditorManagerImpl$special$$inlined$flatMapLatest$1.invokeSuspend(FileEditorManagerImpl.kt:193)
				at kotlinx.coroutines.flow.internal.ChannelFlowTransformLatest$flowCollect$3$1$2.invokeSuspend(Merge.kt:34)
		- "com.intellij.openapi.fileEditor.impl.PsiAwareFileEditorManagerImpl":StandaloneCoroutine{Active}, state: SUSPENDED [ComponentManager(ProjectImpl@1365006477), Dispatchers.Default]
			at kotlinx.coroutines.flow.FlowKt__ChannelsKt.emitAllImpl$FlowKt__ChannelsKt(Channels.kt:36)
			at kotlinx.coroutines.flow.internal.ChannelFlow$collect$2.invokeSuspend(ChannelFlow.kt:123)
			at com.intellij.openapi.fileEditor.impl.FileEditorManagerImpl$special$$inlined$flatMapLatest$2.invokeSuspend(FileEditorManagerImpl.kt:193)
			at kotlinx.coroutines.flow.internal.ChannelFlowTransformLatest$flowCollect$3$1$2.invokeSuspend(Merge.kt:34)
			- "com.intellij.openapi.fileEditor.impl.PsiAwareFileEditorManagerImpl":ProducerCoroutine{Active}, state: SUSPENDED [ComponentManager(ProjectImpl@1365006477), Dispatchers.Default]
				at kotlinx.coroutines.flow.StateFlowImpl.collect(StateFlow.kt:401)
				at kotlinx.coroutines.flow.internal.ChannelFlowTransformLatest$flowCollect$3.invokeSuspend(Merge.kt:27)
				at kotlinx.coroutines.flow.internal.ChannelFlow$collectToFun$1.invokeSuspend(ChannelFlow.kt:60)

It's obvious that the CoroutineName is the name of the standalone coroutine, but it pollutes all its children names in the dump. This will also speed up coroutines a bit, because less elements in the context means less time spent in CoroutineContext.get.

The Shape of the API

I don't see why CoroutineName is propagated to children at all, but I might miss some other use-case, so I'm asking to support a flag: CoroutineName("my coroutine", applyToChildren = false).

dovchinnikov avatar Feb 19 '24 10:02 dovchinnikov