From c0b44475406c4d413c1e94e3683413139f3fa691 Mon Sep 17 00:00:00 2001 From: hyhyf <627680933@qq.com> Date: Mon, 27 Mar 2023 22:15:10 +0800 Subject: [PATCH] Fix a thread leak in `SamplingService` when updated sampling policy in the runtime. (#487) --- CHANGES.md | 1 + .../skywalking/apm/agent/core/sampling/SamplingService.java | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) 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(