Skip to content

Fully copy CoroutineInfo for DebugProbes.dumpCoroutinesInfo, it is re… #1368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 9, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
public final class kotlinx/coroutines/debug/CoroutineInfo {
public final fun component1 ()Lkotlin/coroutines/CoroutineContext;
public final fun copy ()Lkotlinx/coroutines/debug/CoroutineInfo;
public final fun copy (Lkotlin/coroutines/CoroutineContext;Lkotlin/coroutines/jvm/internal/CoroutineStackFrame;J)Lkotlinx/coroutines/debug/CoroutineInfo;
public static synthetic fun copy$default (Lkotlinx/coroutines/debug/CoroutineInfo;Lkotlin/coroutines/CoroutineContext;Lkotlin/coroutines/jvm/internal/CoroutineStackFrame;JILjava/lang/Object;)Lkotlinx/coroutines/debug/CoroutineInfo;
public fun equals (Ljava/lang/Object;)Z
Expand Down
12 changes: 4 additions & 8 deletions kotlinx-coroutines-debug/src/CoroutineInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,10 @@ public data class CoroutineInfo internal constructor(
@JvmField
internal var lastObservedFrame: CoroutineStackFrame? = null

// Copy constructor
internal constructor(coroutine: Continuation<*>, state: CoroutineInfo) : this(
coroutine.context,
state.creationStackBottom,
state.sequenceNumber
) {
_state = state.state
this.lastObservedFrame = state.lastObservedFrame
public fun copy(): CoroutineInfo = CoroutineInfo(context, creationStackBottom, sequenceNumber).also {
it._state = _state
it.lastObservedFrame = lastObservedFrame
it.lastObservedThread = lastObservedThread
}

/**
Expand Down
6 changes: 3 additions & 3 deletions kotlinx-coroutines-debug/src/internal/DebugProbesImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ internal object DebugProbesImpl {
check(isInstalled) { "Debug probes are not installed" }
val jobToStack = capturedCoroutines
.filter { it.delegate.context[Job] != null }
.associateBy({ it.delegate.context[Job]!! }, {it.info})
.associateBy({ it.delegate.context[Job]!! }, { it.info })
return buildString {
job.build(jobToStack, this, "")
}
Expand Down Expand Up @@ -118,7 +118,7 @@ internal object DebugProbesImpl {
public fun dumpCoroutinesInfo(): List<CoroutineInfo> {
check(isInstalled) { "Debug probes are not installed" }
return capturedCoroutines.asSequence()
.map { CoroutineInfo(it.delegate, it.info) }
.map { it.info.copy() } // Copy as CoroutineInfo can be mutated concurrently by DebugProbes
.sortedBy { it.sequenceNumber }
.toList()
}
Expand Down Expand Up @@ -373,7 +373,7 @@ internal object DebugProbesImpl {
private fun <T : Throwable> sanitizeStackTrace(throwable: T): List<StackTraceElement> {
val stackTrace = throwable.stackTrace
val size = stackTrace.size
val probeIndex = stackTrace.indexOfLast { it.className == "kotlin.coroutines.jvm.internal.DebugProbesKt" }
val probeIndex = stackTrace.indexOfLast { it.className == "kotlin.coroutines.jvm.internal.DebugProbesKt" }

if (!DebugProbes.sanitizeStackTraces) {
return List(size - probeIndex) {
Expand Down
11 changes: 11 additions & 0 deletions kotlinx-coroutines-debug/test/RunningThreadStackMergeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,15 @@ class RunningThreadStackMergeTest : DebugTestBase() {
yield()
assertTrue(true)
}

@Test
fun testActiveThread() = runBlocking<Unit> {
launchCoroutine()
awaitCoroutineStarted()
val info = DebugProbes.dumpCoroutinesInfo().find { it.state == State.RUNNING }
assertNotNull(info)
@Suppress("INVISIBLE_MEMBER") // IDEA bug
assertNotNull(info.lastObservedThread)
coroutineBlocker.await()
}
}