From c1ff9135ee6e9013863f7d64102870cab27da2d5 Mon Sep 17 00:00:00 2001 From: cdfive <31885791+cdfive@users.noreply.github.com> Date: Fri, 21 Feb 2020 09:21:30 +0800 Subject: [PATCH] Fix NPE in Tracer when context size exceeds the limit (#1293) --- .../java/com/alibaba/csp/sentinel/Tracer.java | 16 +++------ .../alibaba/csp/sentinel/context/Context.java | 2 +- .../com/alibaba/csp/sentinel/TracerTest.java | 34 +++++++++++++++++++ 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Tracer.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Tracer.java index 08df6b2c..4d0e6da3 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Tracer.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Tracer.java @@ -17,6 +17,7 @@ package com.alibaba.csp.sentinel; import com.alibaba.csp.sentinel.context.Context; import com.alibaba.csp.sentinel.context.ContextUtil; +import com.alibaba.csp.sentinel.context.NullContext; import com.alibaba.csp.sentinel.metric.extension.MetricExtensionProvider; import com.alibaba.csp.sentinel.node.ClusterNode; import com.alibaba.csp.sentinel.node.DefaultNode; @@ -53,17 +54,7 @@ public class Tracer { * @param count exception count to add */ public static void trace(Throwable e, int count) { - if (!shouldTrace(e)) { - return; - } - - Context context = ContextUtil.getContext(); - if (context == null) { - return; - } - - DefaultNode curNode = (DefaultNode)context.getCurNode(); - traceExceptionToNode(e, count, context.getCurEntry(), curNode); + traceContext(e, count, ContextUtil.getContext()); } /** @@ -77,7 +68,8 @@ public class Tracer { if (!shouldTrace(e)) { return; } - if (context == null) { + + if (context == null || context instanceof NullContext) { return; } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/context/Context.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/context/Context.java index 0e857b72..54d5d497 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/context/Context.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/context/Context.java @@ -109,7 +109,7 @@ public class Context { } public Node getCurNode() { - return curEntry.getCurNode(); + return curEntry == null ? null : curEntry.getCurNode(); } public Context setCurNode(Node node) { diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/TracerTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/TracerTest.java index 4b2352e1..2a0e21a6 100644 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/TracerTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/TracerTest.java @@ -1,6 +1,10 @@ package com.alibaba.csp.sentinel; +import com.alibaba.csp.sentinel.context.ContextTestUtil; +import com.alibaba.csp.sentinel.context.ContextUtil; +import org.junit.After; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; /** @@ -8,6 +12,36 @@ import org.junit.Test; */ public class TracerTest extends Tracer { + @Before + public void setUp() { + ContextTestUtil.cleanUpContext(); + ContextTestUtil.resetContextMap(); + } + + @After + public void tearDown() { + ContextTestUtil.cleanUpContext(); + ContextTestUtil.resetContextMap(); + } + + @Test + public void testTraceWhenContextSizeExceedsThreshold() { + int i = 0; + for (; i < Constants.MAX_CONTEXT_NAME_SIZE; i++) { + ContextUtil.enter("test-context-" + i); + ContextUtil.exit(); + } + + try { + ContextUtil.enter("test-context-" + i); + throw new RuntimeException("test"); + } catch (Exception e) { + Tracer.trace(e); + } finally { + ContextUtil.exit(); + } + } + @Test public void setExceptionsToTrace() { Tracer.ignoreClasses = null;