Avoid RejectedExecutionException (#7516)

TIL (in another repo but applicable here)
This commit is contained in:
Trask Stalnaker 2023-01-09 13:22:57 -08:00 committed by GitHub
parent f0bd7c0504
commit ecacd8c86b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 3 deletions

View File

@ -9,6 +9,7 @@ import io.opentelemetry.api.trace.SpanKind;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer; import java.util.function.Consumer;
@ -84,6 +85,7 @@ public final class SupportabilityMetrics {
@SuppressWarnings("CanIgnoreReturnValueSuggester") @SuppressWarnings("CanIgnoreReturnValueSuggester")
private SupportabilityMetrics start() { private SupportabilityMetrics start() {
if (agentDebugEnabled) { if (agentDebugEnabled) {
ScheduledExecutorService executor =
Executors.newScheduledThreadPool( Executors.newScheduledThreadPool(
1, 1,
runnable -> { runnable -> {
@ -91,8 +93,16 @@ public final class SupportabilityMetrics {
result.setDaemon(true); result.setDaemon(true);
result.setContextClassLoader(null); result.setContextClassLoader(null);
return result; return result;
}) });
.scheduleAtFixedRate(this::report, 5, 5, TimeUnit.SECONDS); executor.scheduleAtFixedRate(this::report, 5, 5, TimeUnit.SECONDS);
// the condition below will always be false, but by referencing the executor it ensures the
// executor can't become unreachable in the middle of the scheduleAtFixedRate() method
// execution above (and prior to the task being registered), which can lead to the executor
// being terminated and scheduleAtFixedRate throwing a RejectedExecutionException
// (see https://bugs.openjdk.org/browse/JDK-8145304)
if (executor.isTerminated()) {
throw new AssertionError();
}
} }
return this; return this;
} }