diff --git a/CHANGES.md b/CHANGES.md index 02da739e3..c4f27ee95 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -22,6 +22,7 @@ Release Notes. * Fix thrift plugin generate duplicate traceid when `sendBase` error occurs * Support keep trace profiling when cross-thread. * Fix unexpected whitespace of the command catalogs in several Redis plugins. +* Fix a thread leak in `SamplingService` when updated sampling policy in the runtime. #### Documentation * Update docs of Tracing APIs, reorganize the API docs into six parts. diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/sampling/SamplingService.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/sampling/SamplingService.java index ada982534..dff09fda6 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/sampling/SamplingService.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/sampling/SamplingService.java @@ -51,6 +51,7 @@ public class SamplingService implements BootService { private volatile ScheduledFuture scheduledFuture; private SamplingRateWatcher samplingRateWatcher; + private ScheduledExecutorService service; @Override public void prepare() { @@ -58,6 +59,8 @@ public class SamplingService implements BootService { @Override public void boot() { + service = Executors.newSingleThreadScheduledExecutor( + new DefaultNamedThreadFactory("SamplingService")); samplingRateWatcher = new SamplingRateWatcher("agent.sample_n_per_3_secs", this); ServiceManager.INSTANCE.findService(ConfigurationDiscoveryService.class) .registerAgentConfigChangeWatcher(samplingRateWatcher); @@ -118,8 +121,6 @@ public class SamplingService implements BootService { if (!on) { on = true; this.resetSamplingFactor(); - ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor( - new DefaultNamedThreadFactory("SamplingService")); scheduledFuture = service.scheduleAtFixedRate(new RunnableWithExceptionProtection( this::resetSamplingFactor, t -> LOGGER.error("unexpected exception.", t)), 0, 3, TimeUnit.SECONDS); LOGGER.debug(