Skip to content

Commit b942c73

Browse files
qwwdfsadrecheej
authored andcommitted
Minor improvements of debugging experience (Kotlin#2093)
* Recognizable toString implementation in default dispatchers * Fast-path for disabled debug probes
1 parent c714b65 commit b942c73

File tree

6 files changed

+21
-11
lines changed

6 files changed

+21
-11
lines changed

docs/flow.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ This code produces the following exception:
697697
```text
698698
Exception in thread "main" java.lang.IllegalStateException: Flow invariant is violated:
699699
Flow was collected in [CoroutineId(1), "coroutine#1":BlockingCoroutine{Active}@5511c7f8, BlockingEventLoop@2eac3323],
700-
but emission happened in [CoroutineId(1), "coroutine#1":DispatchedCoroutine{Active}@2dae0000, DefaultDispatcher].
700+
but emission happened in [CoroutineId(1), "coroutine#1":DispatchedCoroutine{Active}@2dae0000, Dispatchers.Default].
701701
Please refer to 'flow' documentation or use 'flowOn' instead
702702
at ...
703703
```

kotlinx-coroutines-core/common/src/Unconfined.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ internal object Unconfined : CoroutineDispatcher() {
2626
"isDispatchNeeded and dispatch calls.")
2727
}
2828

29-
override fun toString(): String = "Unconfined"
29+
override fun toString(): String = "Dispatchers.Unconfined"
3030
}
3131

3232
/**

kotlinx-coroutines-core/jvm/src/debug/internal/DebugProbesImpl.kt

+1
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ internal object DebugProbesImpl {
272272
internal fun probeCoroutineSuspended(frame: Continuation<*>) = updateState(frame, SUSPENDED)
273273

274274
private fun updateState(frame: Continuation<*>, state: String) {
275+
if (!isInstalled) return
275276
// KT-29997 is here only since 1.3.30
276277
if (state == RUNNING && KotlinVersion.CURRENT.isAtLeast(1, 3, 30)) {
277278
val stackFrame = frame as? CoroutineStackFrame ?: return

kotlinx-coroutines-core/jvm/src/scheduling/Dispatcher.kt

+14-8
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@ import kotlin.coroutines.*
1414
* Default instance of coroutine dispatcher.
1515
*/
1616
internal object DefaultScheduler : ExperimentalCoroutineDispatcher() {
17-
val IO = blocking(systemProp(IO_PARALLELISM_PROPERTY_NAME, 64.coerceAtLeast(AVAILABLE_PROCESSORS)))
17+
val IO: CoroutineDispatcher = LimitingDispatcher(
18+
this,
19+
systemProp(IO_PARALLELISM_PROPERTY_NAME, 64.coerceAtLeast(AVAILABLE_PROCESSORS)),
20+
"Dispatchers.IO",
21+
TASK_PROBABLY_BLOCKING
22+
)
1823

1924
override fun close() {
20-
throw UnsupportedOperationException("$DEFAULT_SCHEDULER_NAME cannot be closed")
25+
throw UnsupportedOperationException("$DEFAULT_DISPATCHER_NAME cannot be closed")
2126
}
2227

23-
override fun toString(): String = DEFAULT_SCHEDULER_NAME
28+
override fun toString(): String = DEFAULT_DISPATCHER_NAME
2429

2530
@InternalCoroutinesApi
2631
@Suppress("UNUSED")
@@ -85,7 +90,7 @@ public open class ExperimentalCoroutineDispatcher(
8590
*/
8691
public fun blocking(parallelism: Int = BLOCKING_DEFAULT_PARALLELISM): CoroutineDispatcher {
8792
require(parallelism > 0) { "Expected positive parallelism level, but have $parallelism" }
88-
return LimitingDispatcher(this, parallelism, TASK_PROBABLY_BLOCKING)
93+
return LimitingDispatcher(this, parallelism, null, TASK_PROBABLY_BLOCKING)
8994
}
9095

9196
/**
@@ -98,7 +103,7 @@ public open class ExperimentalCoroutineDispatcher(
98103
public fun limited(parallelism: Int): CoroutineDispatcher {
99104
require(parallelism > 0) { "Expected positive parallelism level, but have $parallelism" }
100105
require(parallelism <= corePoolSize) { "Expected parallelism level lesser than core pool size ($corePoolSize), but have $parallelism" }
101-
return LimitingDispatcher(this, parallelism, TASK_NON_BLOCKING)
106+
return LimitingDispatcher(this, parallelism, null, TASK_NON_BLOCKING)
102107
}
103108

104109
internal fun dispatchWithContext(block: Runnable, context: TaskContext, tailDispatch: Boolean) {
@@ -130,8 +135,9 @@ public open class ExperimentalCoroutineDispatcher(
130135
}
131136

132137
private class LimitingDispatcher(
133-
val dispatcher: ExperimentalCoroutineDispatcher,
134-
val parallelism: Int,
138+
private val dispatcher: ExperimentalCoroutineDispatcher,
139+
private val parallelism: Int,
140+
private val name: String?,
135141
override val taskMode: Int
136142
) : ExecutorCoroutineDispatcher(), TaskContext, Executor {
137143

@@ -190,7 +196,7 @@ private class LimitingDispatcher(
190196
}
191197

192198
override fun toString(): String {
193-
return "${super.toString()}[dispatcher = $dispatcher]"
199+
return name ?: "${super.toString()}[dispatcher = $dispatcher]"
194200
}
195201

196202
/**

kotlinx-coroutines-core/jvm/src/scheduling/Tasks.kt

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import java.util.concurrent.*
1111

1212
// TODO most of these fields will be moved to 'object ExperimentalDispatcher'
1313

14+
// User-visible name
15+
internal const val DEFAULT_DISPATCHER_NAME = "Dispatchers.Default"
16+
// Internal debuggability name + thread name prefixes
1417
internal const val DEFAULT_SCHEDULER_NAME = "DefaultDispatcher"
1518

1619
// 100us as default

kotlinx-coroutines-core/jvm/test/guide/test/FlowGuideTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class FlowGuideTest {
151151
test("ExampleFlow14") { kotlinx.coroutines.guide.exampleFlow14.main() }.verifyExceptions(
152152
"Exception in thread \"main\" java.lang.IllegalStateException: Flow invariant is violated:",
153153
"\t\tFlow was collected in [CoroutineId(1), \"coroutine#1\":BlockingCoroutine{Active}@5511c7f8, BlockingEventLoop@2eac3323],",
154-
"\t\tbut emission happened in [CoroutineId(1), \"coroutine#1\":DispatchedCoroutine{Active}@2dae0000, DefaultDispatcher].",
154+
"\t\tbut emission happened in [CoroutineId(1), \"coroutine#1\":DispatchedCoroutine{Active}@2dae0000, Dispatchers.Default].",
155155
"\t\tPlease refer to 'flow' documentation or use 'flowOn' instead",
156156
"\tat ..."
157157
)

0 commit comments

Comments
 (0)