Skip to content

Commit c0b4447

Browse files
authored
Fix a thread leak in SamplingService when updated sampling policy in the runtime. (#487)
1 parent e91514c commit c0b4447

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Release Notes.
2222
* Fix thrift plugin generate duplicate traceid when `sendBase` error occurs
2323
* Support keep trace profiling when cross-thread.
2424
* Fix unexpected whitespace of the command catalogs in several Redis plugins.
25+
* Fix a thread leak in `SamplingService` when updated sampling policy in the runtime.
2526

2627
#### Documentation
2728
* Update docs of Tracing APIs, reorganize the API docs into six parts.

apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/sampling/SamplingService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,16 @@ public class SamplingService implements BootService {
5151
private volatile ScheduledFuture<?> scheduledFuture;
5252

5353
private SamplingRateWatcher samplingRateWatcher;
54+
private ScheduledExecutorService service;
5455

5556
@Override
5657
public void prepare() {
5758
}
5859

5960
@Override
6061
public void boot() {
62+
service = Executors.newSingleThreadScheduledExecutor(
63+
new DefaultNamedThreadFactory("SamplingService"));
6164
samplingRateWatcher = new SamplingRateWatcher("agent.sample_n_per_3_secs", this);
6265
ServiceManager.INSTANCE.findService(ConfigurationDiscoveryService.class)
6366
.registerAgentConfigChangeWatcher(samplingRateWatcher);
@@ -118,8 +121,6 @@ public void handleSamplingRateChanged() {
118121
if (!on) {
119122
on = true;
120123
this.resetSamplingFactor();
121-
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(
122-
new DefaultNamedThreadFactory("SamplingService"));
123124
scheduledFuture = service.scheduleAtFixedRate(new RunnableWithExceptionProtection(
124125
this::resetSamplingFactor, t -> LOGGER.error("unexpected exception.", t)), 0, 3, TimeUnit.SECONDS);
125126
LOGGER.debug(

0 commit comments

Comments
 (0)