From a452c42f0307fb9396215666b0ef3917febc3213 Mon Sep 17 00:00:00 2001 From: forrestwang Date: Fri, 19 Apr 2024 07:23:49 +0800 Subject: [PATCH] Fix NPE in handleMethodException method of apm-jdk-threadpool-plugin. (#681) --- .../AbstractThreadingPoolInterceptor.java | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/apm-sniffer/bootstrap-plugins/jdk-threadpool-plugin/src/main/java/org/apache/skywalking/apm/plugin/AbstractThreadingPoolInterceptor.java b/apm-sniffer/bootstrap-plugins/jdk-threadpool-plugin/src/main/java/org/apache/skywalking/apm/plugin/AbstractThreadingPoolInterceptor.java index c72c1424b5..e11c260f42 100644 --- a/apm-sniffer/bootstrap-plugins/jdk-threadpool-plugin/src/main/java/org/apache/skywalking/apm/plugin/AbstractThreadingPoolInterceptor.java +++ b/apm-sniffer/bootstrap-plugins/jdk-threadpool-plugin/src/main/java/org/apache/skywalking/apm/plugin/AbstractThreadingPoolInterceptor.java @@ -28,22 +28,11 @@ public abstract class AbstractThreadingPoolInterceptor implements InstanceMethodsAroundInterceptor { @Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, MethodInterceptResult result) throws Throwable { - if (!ContextManager.isActive()) { - return; - } - - if (allArguments == null || allArguments.length < 1) { - return; - } - - Object argument = allArguments[0]; - - // Avoid duplicate enhancement, such as the case where it has already been enhanced by RunnableWrapper or CallableWrapper with toolkit. - if (argument instanceof EnhancedInstance && ((EnhancedInstance) argument).getSkyWalkingDynamicField() instanceof ContextSnapshot) { + if (notToEnhance(allArguments)) { return; } - Object wrappedObject = wrap(argument); + Object wrappedObject = wrap(allArguments[0]); if (wrappedObject != null) { allArguments[0] = wrappedObject; } @@ -63,10 +52,25 @@ public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allA @Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Throwable t) { - if (!ContextManager.isActive()) { + if (notToEnhance(allArguments)) { return; } ContextManager.activeSpan().log(t); } + + private boolean notToEnhance(Object[] allArguments) { + if (!ContextManager.isActive()) { + return true; + } + + if (allArguments == null || allArguments.length < 1) { + return true; + } + + Object argument = allArguments[0]; + + // Avoid duplicate enhancement, such as the case where it has already been enhanced by RunnableWrapper or CallableWrapper with toolkit. + return argument instanceof EnhancedInstance && ((EnhancedInstance) argument).getSkyWalkingDynamicField() instanceof ContextSnapshot; + } }