Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metrics: use atomic type (#27121) #384

Merged
merged 1 commit into from
Nov 30, 2023

Conversation

minh-bq
Copy link
Collaborator

@minh-bq minh-bq commented Nov 29, 2023

We observe the data race warning when enabling metric

WARNING: DATA RACE
Read at 0x00c02cc7fe90 by goroutine 49:
  github.com/ethereum/go-ethereum/metrics.(*StandardMeter).Snapshot()
      github.com/ethereum/go-ethereum/metrics/meter.go:244 +0x6e
  github.com/ethereum/go-ethereum/metrics/prometheus.Handler.func1()
      github.com/ethereum/go-ethereum/metrics/prometheus/prometheus.go:55 +0x3c2

Previous write at 0x00c02cc7fe90 by goroutine 251:
  sync/atomic.AddInt64()
      runtime/race_amd64.s:289 +0xb
  sync/atomic.AddInt64()
      <autogenerated>:1 +0x1b
  github.com/ethereum/go-ethereum/p2p.(*rlpxTransport).WriteMsg()
      github.com/ethereum/go-ethereum/p2p/transport.go:103 +0x4c3
  github.com/ethereum/go-ethereum/p2p.(*conn).WriteMsg()
      <autogenerated>:1 +0xb9

The data race happens on StandardMeter.snapshot.temp field. This field access does not follow the StandardMeter.lock but use atomic access. The write side in StandardMeter.Mark is atomic, however, the read side in StandardMeter.Snapshot is not.

We cherry-pick this commit to correctly use atomic read of StandardMeter.snapshot.temp in StandardMeter.Snapshot. This commit also replaces the use of atomic.Load/StoreInt64 with atomic.Int64 type.

@minh-bq minh-bq merged commit d03c78b into axieinfinity:master Nov 30, 2023
1 check passed
@minh-bq minh-bq deleted the fix/meter-data-race branch November 30, 2023 06:35
minh-bq added a commit to minh-bq/ronin that referenced this pull request Feb 20, 2024
We observe the data race warning when enabling metric

  WARNING: DATA RACE
  Read at 0x00c02cc7fe90 by goroutine 49:
    github.com/ethereum/go-ethereum/metrics.(*StandardMeter).Snapshot()
        github.com/ethereum/go-ethereum/metrics/meter.go:244 +0x6e
    github.com/ethereum/go-ethereum/metrics/prometheus.Handler.func1()
        github.com/ethereum/go-ethereum/metrics/prometheus/prometheus.go:55 +0x3c2

  Previous write at 0x00c02cc7fe90 by goroutine 251:
    sync/atomic.AddInt64()
        runtime/race_amd64.s:289 +0xb
    sync/atomic.AddInt64()
        <autogenerated>:1 +0x1b
    github.com/ethereum/go-ethereum/p2p.(*rlpxTransport).WriteMsg()
        github.com/ethereum/go-ethereum/p2p/transport.go:103 +0x4c3
    github.com/ethereum/go-ethereum/p2p.(*conn).WriteMsg()
        <autogenerated>:1 +0xb9

The data race happens on StandardMeter.snapshot.temp field. This field access does not follow the StandardMeter.lock but use atomic access. The write side in StandardMeter.Mark is atomic, however, the read side in StandardMeter.Snapshot is not.

We cherry-pick this commit to correctly use atomic read of StandardMeter.snapshot.temp in StandardMeter.Snapshot. This commit also replaces the use of atomic.Load/StoreInt64 with atomic.Int64 type.

Co-authored-by: s7v7nislands <s7v7nislands@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants