Skip to content

Commit d63e3c3

Browse files
committed
Rethrow failure on main thread
Previously, if a failure occurred when evaluating conditions on a separate thread, an NPE would occur on the main thread as the expected array of outcomes was null. This commit avoids the NPE and the lack of error reporting by rethrowing on the main thread any failure that occurs on the separate thread that's spawned to parallelize the evaluation. Closes gh-41492
1 parent 61a3b73 commit d63e3c3

File tree

1 file changed

+15
-2
lines changed
  • spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition

1 file changed

+15
-2
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnClassCondition.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@
2929
import org.springframework.core.annotation.Order;
3030
import org.springframework.core.type.AnnotatedTypeMetadata;
3131
import org.springframework.util.MultiValueMap;
32+
import org.springframework.util.ReflectionUtils;
3233
import org.springframework.util.StringUtils;
3334

3435
/**
@@ -142,8 +143,17 @@ private static final class ThreadedOutcomesResolver implements OutcomesResolver
142143

143144
private volatile ConditionOutcome[] outcomes;
144145

146+
private volatile Throwable failure;
147+
145148
private ThreadedOutcomesResolver(OutcomesResolver outcomesResolver) {
146-
this.thread = new Thread(() -> this.outcomes = outcomesResolver.resolveOutcomes());
149+
this.thread = new Thread(() -> {
150+
try {
151+
this.outcomes = outcomesResolver.resolveOutcomes();
152+
}
153+
catch (Throwable ex) {
154+
this.failure = ex;
155+
}
156+
});
147157
this.thread.start();
148158
}
149159

@@ -155,6 +165,9 @@ public ConditionOutcome[] resolveOutcomes() {
155165
catch (InterruptedException ex) {
156166
Thread.currentThread().interrupt();
157167
}
168+
if (this.failure != null) {
169+
ReflectionUtils.rethrowRuntimeException(this.failure);
170+
}
158171
return this.outcomes;
159172
}
160173

0 commit comments

Comments
 (0)