In addition to our previous issue report, we have found three more races in your repo. When we run testMetricsProcessor, the main thread executes Javametrics.getInstance().addListener(mp). That triggers the initialization of JavametricsImpl singleton, and the drain task scheduled via scheduleAtFixedRate is run on a background thread:
|
exec.scheduleAtFixedRate(this::drain, collectionInterval, collectionInterval, TimeUnit.SECONDS); |
The drain path follows the call sequence of (drain -> drainBatched -> emit -> receiveData -> receive -> processData) and processes buffered JSON data.
Meanwhile, the main thread sleeps for three seconds and then calls printSummary. If printSummary runs while the background thread is still updating MetricsContext, the main thread can read gcEvents:
while the background thread is updating the same variable in aggregateGc:
By the same reasoning, cpuEvents and cpuSystem, which are updated in aggregateCpu, are also likely to be racy.
In addition to our previous issue report, we have found three more races in your repo. When we run
testMetricsProcessor, the main thread executesJavametrics.getInstance().addListener(mp). That triggers the initialization ofJavametricsImplsingleton, and the drain task scheduled viascheduleAtFixedRateis run on a background thread:javametrics/javaagent/src/main/java/com/ibm/javametrics/agent/JavaAgent.java
Line 47 in f7e1a5e
The drain path follows the call sequence of (
drain->drainBatched->emit->receiveData->receive->processData) and processes buffered JSON data.Meanwhile, the main thread sleeps for three seconds and then calls
printSummary. IfprintSummaryruns while the background thread is still updatingMetricsContext, the main thread can readgcEvents:javametrics/javaagent/src/main/java/com/ibm/javametrics/analysis/MetricsContext.java
Line 110 in f7e1a5e
while the background thread is updating the same variable in
aggregateGc:javametrics/javaagent/src/main/java/com/ibm/javametrics/analysis/MetricsContext.java
Line 82 in f7e1a5e
By the same reasoning,
cpuEventsandcpuSystem, which are updated inaggregateCpu, are also likely to be racy.