You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2020/03/11 05:54:00 UTC

[skywalking] 01/01: Fix NoSuchElement exception for lazy injection.

This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch fix-lazy-inject
in repository https://gitbox.apache.org/repos/asf/skywalking.git

commit c0a293f4456447684c250b313edc1c9e7bb35939
Author: Wu Sheng <wu...@foxmail.com>
AuthorDate: Wed Mar 11 13:53:34 2020 +0800

    Fix NoSuchElement exception for lazy injection.
---
 .../skywalking/apm/agent/core/context/TracingContext.java     | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/TracingContext.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/TracingContext.java
index bbb7b51..19538ee 100644
--- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/TracingContext.java
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/TracingContext.java
@@ -79,6 +79,12 @@ public class TracingContext implements AbstractTracerContext {
      * LinkedList#getLast()} instead of {@link #pop()}, {@link #push(AbstractSpan)}, {@link #peek()}
      */
     private LinkedList<AbstractSpan> activeSpanStack = new LinkedList<>();
+    /**
+     * @since 7.0.0 SkyWalking support lazy injection through {@link ExitTypeSpan#inject(ContextCarrier)}. Due to that,
+     * the {@link #activeSpanStack} could be blank by then, this is a pointer forever to the first span, even the main
+     * thread tracing has been finished.
+     */
+    private AbstractSpan firstSpan = null;
 
     /**
      * A counter for the next span.
@@ -636,6 +642,9 @@ public class TracingContext implements AbstractTracerContext {
      * @param span the {@code span} to push
      */
     private AbstractSpan push(AbstractSpan span) {
+        if (firstSpan == null) {
+            firstSpan = span;
+        }
         activeSpanStack.addLast(span);
         return span;
     }
@@ -651,7 +660,7 @@ public class TracingContext implements AbstractTracerContext {
     }
 
     private AbstractSpan first() {
-        return activeSpanStack.getFirst();
+        return firstSpan;
     }
 
     private boolean isLimitMechanismWorking() {