Skip to content

Commit

Permalink
Rethrow failure on main thread
Browse files Browse the repository at this point in the history
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
  • Loading branch information
wilkinsona committed Jul 15, 2024
1 parent 61a3b73 commit d63e3c3
Showing 1 changed file with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,6 +29,7 @@
import org.springframework.core.annotation.Order;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;

/**
Expand Down Expand Up @@ -142,8 +143,17 @@ private static final class ThreadedOutcomesResolver implements OutcomesResolver

private volatile ConditionOutcome[] outcomes;

private volatile Throwable failure;

private ThreadedOutcomesResolver(OutcomesResolver outcomesResolver) {
this.thread = new Thread(() -> this.outcomes = outcomesResolver.resolveOutcomes());
this.thread = new Thread(() -> {
try {
this.outcomes = outcomesResolver.resolveOutcomes();
}
catch (Throwable ex) {
this.failure = ex;
}
});
this.thread.start();
}

Expand All @@ -155,6 +165,9 @@ public ConditionOutcome[] resolveOutcomes() {
catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
if (this.failure != null) {
ReflectionUtils.rethrowRuntimeException(this.failure);
}
return this.outcomes;
}

Expand Down

0 comments on commit d63e3c3

Please sign in to comment.