question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Kotlin Coroutines] why to end span while restoring thread context?

See original GitHub issue

Describe the bug Use Spring Cloud Sleuth 3.1.3

KotlinContextElement.java

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:

Screenshot 2022-08-30 at 19 38 11

Expected: duration of root’s span more than 200ms

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
marcingrzejszczakcommented, Sep 1, 2022

Since today I guess we’ll be doing a release I’ve fixed this

1reaction
marcingrzejszczakcommented, Aug 31, 2022

That doesn’t sound good. I mean we should propagate the context and we should only close the scope after the code was executed.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found