diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/ContextManager.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/ContextManager.java index 9c3f48dd5..90ffdb19a 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/ContextManager.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/ContextManager.java @@ -150,11 +150,12 @@ public class ContextManager implements BootService { } public static AbstractTracerContext awaitFinishAsync(AbstractSpan span) { - AbstractSpan activeSpan = activeSpan(); + final AbstractTracerContext context = get(); + AbstractSpan activeSpan = context.activeSpan(); if (span != activeSpan) { throw new RuntimeException("Span is not the active in current context."); } - return get().awaitFinishAsync(); + return context.awaitFinishAsync(); } /** @@ -165,12 +166,22 @@ public class ContextManager implements BootService { return get().activeSpan(); } + /** + * Recommend use ContextManager::stopSpan(AbstractSpan span), because in that way, + * the TracingContext core could verify this span is the active one, in order to avoid stop unexpected span. + * If the current span is hard to get or only could get by low-performance way, this stop way is still acceptable. + */ public static void stopSpan() { - stopSpan(activeSpan()); + final AbstractTracerContext context = get(); + stopSpan(context.activeSpan(),context); } public static void stopSpan(AbstractSpan span) { - if (get().stopSpan(span)) { + stopSpan(span, get()); + } + + private static void stopSpan(AbstractSpan span, final AbstractTracerContext context) { + if (context.stopSpan(span)) { CONTEXT.remove(); RUNTIME_CONTEXT.remove(); }