Ensures that we close the scope when working with Kotlin; fixes gh-2197

This commit is contained in:
Marcin Grzejszczak
2022-09-01 11:01:19 +02:00
parent 64a0be4944
commit b856c2c44e
2 changed files with 9 additions and 1 deletions

View File

@@ -65,7 +65,10 @@ class KotlinContextElement implements ThreadContextElement<SpanAndScope> {
@Override
public void restoreThreadContext(CoroutineContext coroutineContext, SpanAndScope spanAndScope) {
spanAndScope.close();
Tracer.SpanInScope scope = spanAndScope.getScope();
if (scope != null) {
scope.close();
}
}
@Override

View File

@@ -29,6 +29,7 @@ import org.springframework.cloud.sleuth.Span
import org.springframework.cloud.sleuth.TraceContext
import org.springframework.cloud.sleuth.Tracer
import org.springframework.cloud.sleuth.tracer.SimpleCurrentTraceContext
import org.springframework.cloud.sleuth.tracer.SimpleSpan
import org.springframework.cloud.sleuth.tracer.SimpleTracer
import reactor.util.context.Context
@@ -45,15 +46,19 @@ internal class AsContextElementKtTests {
GlobalScope.launch(asContextElement) {
spanInGlobalScopeLaunch = coroutineContext.currentSpan()
then((spanInGlobalScopeLaunch as SimpleSpan).ended).isFalse()
}
GlobalScope.async(asContextElement) {
spanInGlobalScopeAsync = coroutineContext.currentSpan()
then((spanInGlobalScopeAsync as SimpleSpan).ended).isFalse()
}.await()
inScope.close();
then(spanInGlobalScopeLaunch).isSameAs(nextSpan)
then(spanInGlobalScopeAsync).isSameAs(nextSpan)
then((spanInGlobalScopeLaunch as SimpleSpan).ended).isFalse()
then((spanInGlobalScopeAsync as SimpleSpan).ended).isFalse()
}
@Test