Skip to content

Commit ecacd8c

Browse files
authored
Avoid RejectedExecutionException (#7516)
TIL (in another repo but applicable here)
1 parent f0bd7c0 commit ecacd8c

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/internal/SupportabilityMetrics.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.concurrent.ConcurrentHashMap;
1010
import java.util.concurrent.ConcurrentMap;
1111
import java.util.concurrent.Executors;
12+
import java.util.concurrent.ScheduledExecutorService;
1213
import java.util.concurrent.TimeUnit;
1314
import java.util.concurrent.atomic.AtomicLong;
1415
import java.util.function.Consumer;
@@ -84,15 +85,24 @@ void report() {
8485
@SuppressWarnings("CanIgnoreReturnValueSuggester")
8586
private SupportabilityMetrics start() {
8687
if (agentDebugEnabled) {
87-
Executors.newScheduledThreadPool(
88+
ScheduledExecutorService executor =
89+
Executors.newScheduledThreadPool(
8890
1,
8991
runnable -> {
9092
Thread result = new Thread(runnable, "supportability_metrics_reporter");
9193
result.setDaemon(true);
9294
result.setContextClassLoader(null);
9395
return result;
94-
})
95-
.scheduleAtFixedRate(this::report, 5, 5, TimeUnit.SECONDS);
96+
});
97+
executor.scheduleAtFixedRate(this::report, 5, 5, TimeUnit.SECONDS);
98+
// the condition below will always be false, but by referencing the executor it ensures the
99+
// executor can't become unreachable in the middle of the scheduleAtFixedRate() method
100+
// execution above (and prior to the task being registered), which can lead to the executor
101+
// being terminated and scheduleAtFixedRate throwing a RejectedExecutionException
102+
// (see https://bugs.openjdk.org/browse/JDK-8145304)
103+
if (executor.isTerminated()) {
104+
throw new AssertionError();
105+
}
96106
}
97107
return this;
98108
}

0 commit comments

Comments
 (0)