From 011eb938ac9ac60836a5a422ef6b783aafa899a8 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Mon, 3 Jan 2022 14:27:14 +0100 Subject: [PATCH] Added NPE guard for null async executor; fixes gh-2094 --- .../instrument/async/LazyTraceAsyncCustomizer.java | 10 +++++++--- .../instrument/async/LazyTraceAsyncCustomizerTest.java | 9 +++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/spring-cloud-sleuth-instrumentation/src/main/java/org/springframework/cloud/sleuth/instrument/async/LazyTraceAsyncCustomizer.java b/spring-cloud-sleuth-instrumentation/src/main/java/org/springframework/cloud/sleuth/instrument/async/LazyTraceAsyncCustomizer.java index e7db11f4e..21d80a42c 100644 --- a/spring-cloud-sleuth-instrumentation/src/main/java/org/springframework/cloud/sleuth/instrument/async/LazyTraceAsyncCustomizer.java +++ b/spring-cloud-sleuth-instrumentation/src/main/java/org/springframework/cloud/sleuth/instrument/async/LazyTraceAsyncCustomizer.java @@ -44,10 +44,14 @@ public class LazyTraceAsyncCustomizer extends AsyncConfigurerSupport { @Override public Executor getAsyncExecutor() { - if (this.delegate.getAsyncExecutor() instanceof LazyTraceExecutor) { - return this.delegate.getAsyncExecutor(); + Executor executor = this.delegate.getAsyncExecutor(); + if (executor instanceof LazyTraceExecutor) { + return executor; } - return LazyTraceExecutor.wrap(this.beanFactory, this.delegate.getAsyncExecutor()); + else if (executor == null) { + return null; + } + return LazyTraceExecutor.wrap(this.beanFactory, executor); } @Override diff --git a/spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/instrument/async/LazyTraceAsyncCustomizerTest.java b/spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/instrument/async/LazyTraceAsyncCustomizerTest.java index 48a3d0fad..2e08a89f5 100644 --- a/spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/instrument/async/LazyTraceAsyncCustomizerTest.java +++ b/spring-cloud-sleuth-instrumentation/src/test/java/org/springframework/cloud/sleuth/instrument/async/LazyTraceAsyncCustomizerTest.java @@ -54,4 +54,13 @@ public class LazyTraceAsyncCustomizerTest { BDDAssertions.then(executor).isExactlyInstanceOf(LazyTraceExecutor.class); } + @Test + public void should_return_null_when_executor_null() throws Exception { + BDDMockito.given(this.asyncConfigurer.getAsyncExecutor()).willReturn(null); + + Executor executor = this.lazyTraceAsyncCustomizer.getAsyncExecutor(); + + BDDAssertions.then(executor).isNull(); + } + }