Skip to content

potential data races #2 #115

Description

@dracestudy

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions