[Kotlin Coroutines] why to end span while restoring thread context?
See original GitHub issueDescribe the bug Use Spring Cloud Sleuth 3.1.3
The current behaviour confused me: the span is always closed while restoring thread context.
Example:
@Autowired
private lateinit var tracer: Tracer
@Test
fun spanTest() = runBlocking<Unit> {
val root = tracer.nextSpan().name("root")
val rootSpanInScope = tracer.withSpan(root.start())
try {
rootSpanInScope.use {
delay(100)
withContext(tracer.asContextElement()) {
delay(100)
}
}
} finally {
root.end()
}
}
Zipkin’s visualization:

Expected: duration of root’s span more than 200ms
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Coroutine context and dispatchers - Kotlin
Coroutines can suspend on one thread and resume on another thread. Even with a single-threaded dispatcher it might be hard to figure out...
Read more >Kotlin Coroutines - Magda Miu
The compiler empowers the continuations to implement the mechanism of the coroutines, to switch the context, the threads and to restore the ...
Read more >Use Kotlin coroutines with lifecycle-aware components
A ViewModelScope is defined for each ViewModel in your app. Any coroutine launched in this scope is automatically canceled if the ViewModel is...
Read more >The Beginner's Guide to Kotlin Coroutine Internals
Compared to a Java thread, a Kotlin coroutine has a smaller memory footprint and lower overhead in context-switching.
Read more >Design of Kotlin Coroutines - Droidcon
When a coroutine is suspended, that thread is free for other coroutines. The continuation of the coroutine doesn't have to be on the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Since today I guess we’ll be doing a release I’ve fixed this
That doesn’t sound good. I mean we should propagate the context and we should only close the scope after the code was executed.