Skip to content

Instantly share code, notes, and snippets.

@vsalavatov
Last active December 16, 2024 11:36
Show Gist options
  • Save vsalavatov/4d609f8f163840525f3346985158400e to your computer and use it in GitHub Desktop.
Save vsalavatov/4d609f8f163840525f3346985158400e to your computer and use it in GitHub Desktop.
DebugProbesContext.kt
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.debug.DebugProbes
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
fun main() {
DebugProbes.sanitizeStackTraces = false
DebugProbes.enableCreationStackTraces = false
DebugProbes.install()
runBlocking {
val b1 = CompletableDeferred<Unit>()
val b2 = CompletableDeferred<Unit>()
println("runBlocking context: ${currentCoroutineContext()}")
launch { // in the context of runBlocking
withContext(Dispatchers.IO + CoroutineName("A")) {
println("launched job context: ${currentCoroutineContext()}")
withContext(CoroutineName("A inner") + Dispatchers.Default.limitedParallelism(1)) {
println("inner launched job context: ${currentCoroutineContext()}")
b1.complete(Unit)
b2.await()
}
}
}
b1.await()
println("debug probes")
println(DebugProbes.dumpCoroutines())
}
}
Subject: [PATCH] patch
---
Index: kotlinx-coroutines-core/src/jvmMain/java/internal/StackTraceRecovery.common.jvm.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/kotlinx-coroutines-core/src/jvmMain/java/internal/StackTraceRecovery.common.jvm.kt b/kotlinx-coroutines-core/src/jvmMain/java/internal/StackTraceRecovery.common.jvm.kt
new file mode 100644
--- /dev/null (date 1734348688611)
+++ b/kotlinx-coroutines-core/src/jvmMain/java/internal/StackTraceRecovery.common.jvm.kt (date 1734348688611)
@@ -0,0 +1,10 @@
+package kotlinx.coroutines.internal
+
+import java.lang.StackTraceElement
+import kotlin.coroutines.Continuation
+import kotlin.coroutines.CoroutineContext
+
+actual fun makeStackTraceElement(context: CoroutineContext, uCont: Continuation<*>): kotlinx.coroutines.internal.StackTraceElement? {
+ val frame = (uCont as? CoroutineStackFrame)?.getStackTraceElement()
+ return StackTraceElement(context.toString(), "", frame?.fileName ?: "", frame?.lineNumber ?: 0)
+}
\ No newline at end of file
Index: kotlinx-coroutines-core/common/src/internal/StackTraceRecovery.common.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/kotlinx-coroutines-core/common/src/internal/StackTraceRecovery.common.kt b/kotlinx-coroutines-core/common/src/internal/StackTraceRecovery.common.kt
--- a/kotlinx-coroutines-core/common/src/internal/StackTraceRecovery.common.kt (revision 6c6df2b850382887462eeaf51f21f58bd982491d)
+++ b/kotlinx-coroutines-core/common/src/internal/StackTraceRecovery.common.kt (date 1734348688603)
@@ -41,6 +41,8 @@
internal expect class StackTraceElement
+internal expect fun makeStackTraceElement(context: CoroutineContext, uCont: Continuation<*>): StackTraceElement?
+
internal expect interface CoroutineStackFrame {
public val callerFrame: CoroutineStackFrame?
public fun getStackTraceElement(): StackTraceElement?
Index: kotlinx-coroutines-core/common/src/internal/Scopes.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/kotlinx-coroutines-core/common/src/internal/Scopes.kt b/kotlinx-coroutines-core/common/src/internal/Scopes.kt
--- a/kotlinx-coroutines-core/common/src/internal/Scopes.kt (revision 6c6df2b850382887462eeaf51f21f58bd982491d)
+++ b/kotlinx-coroutines-core/common/src/internal/Scopes.kt (date 1734348688615)
@@ -14,7 +14,7 @@
) : AbstractCoroutine<T>(context, true, true), CoroutineStackFrame {
final override val callerFrame: CoroutineStackFrame? get() = uCont as? CoroutineStackFrame
- final override fun getStackTraceElement(): StackTraceElement? = null
+ final override fun getStackTraceElement(): StackTraceElement? = makeStackTraceElement(context, uCont)
final override val isScopedCoroutine: Boolean get() = true
runBlocking context: [BlockingCoroutine{Active}@247310d0, BlockingEventLoop@1033576a]
launched job context: [CoroutineName(A), DispatchedCoroutine{Active}@78d14e01, Dispatchers.IO]
inner launched job context: [CoroutineName(A inner), DispatchedCoroutine{Active}@93a07fa, Dispatchers.Default.limitedParallelism(1)]
debug probes
Coroutines dump 2024/12/16 12:33:55
Coroutine BlockingCoroutine{Active}@247310d0, state: RUNNING
at java.base/java.lang.Thread.getStackTrace(Thread.java:2450)
at kotlinx.coroutines.debug.internal.DebugProbesImpl.enhanceStackTraceWithThreadDumpImpl(DebugProbesImpl.kt:332)
at kotlinx.coroutines.debug.internal.DebugProbesImpl.dumpCoroutinesSynchronized(DebugProbesImpl.kt:287)
at kotlinx.coroutines.debug.internal.DebugProbesImpl.dumpCoroutines(DebugProbesImpl.kt:259)
at kotlinx.coroutines.debug.DebugProbes.dumpCoroutines(DebugProbes.kt:181)
at kotlinx.coroutines.debug.DebugProbes.dumpCoroutines$default(DebugProbes.kt:181)
at DebugProbesContextKt$main$1.invokeSuspend(DebugProbesContext.kt:30)
Coroutine StandaloneCoroutine{Active}@1500b2f3, state: SUSPENDED
at DebugProbesContextKt$main$1$1$1$1.invokeSuspend(DebugProbesContext.kt:24)
at [CoroutineName(A inner), DispatchedCoroutine{Active}@93a07fa, Dispatchers.Default.limitedParallelism(1)].(DebugProbesContext.kt:21)
at DebugProbesContextKt$main$1$1$1.invokeSuspend(DebugProbesContext.kt:21)
at [CoroutineName(A), DispatchedCoroutine{Active}@78d14e01, Dispatchers.IO].(DebugProbesContext.kt:19)
at DebugProbesContextKt$main$1$1.invokeSuspend(DebugProbesContext.kt:19)kotlin.Unit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment