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

Data race #1526

Closed
1 task done
tossp opened this issue Sep 13, 2023 · 0 comments · Fixed by #1527
Closed
1 task done

Data race #1526

tossp opened this issue Sep 13, 2023 · 0 comments · Fixed by #1527

Comments

@tossp
Copy link
Contributor

tossp commented Sep 13, 2023

Describe the bug
The loadErr variable is read and written simultaneously

var (
loadErr error
loadAvg1M float64 = 0.0
loadAvg5M float64 = 0.0
loadAvg15M float64 = 0.0
loadAvgMutex sync.RWMutex
loadAvgGoroutineOnce sync.Once
)

f := func() {
currentLoad, err = counter.GetValue()
loadErr = err
loadAvgMutex.Lock()
loadAvg1M = loadAvg1M*loadAvgFactor1M + currentLoad*(1-loadAvgFactor1M)
loadAvg5M = loadAvg5M*loadAvgFactor5M + currentLoad*(1-loadAvgFactor5M)
loadAvg15M = loadAvg15M*loadAvgFactor15M + currentLoad*(1-loadAvgFactor15M)
loadAvgMutex.Unlock()
}

func AvgWithContext(ctx context.Context) (*AvgStat, error) {
loadAvgGoroutineOnce.Do(func() {
go loadAvgGoroutine(ctx)
})
loadAvgMutex.RLock()
defer loadAvgMutex.RUnlock()
ret := AvgStat{
Load1: loadAvg1M,
Load5: loadAvg5M,
Load15: loadAvg15M,
}
return &ret, loadErr
}

==================
WARNING: DATA RACE
Write at 0x000143310d10 by goroutine 33:
  github.com/shirou/gopsutil/v3/load.loadAvgGoroutine.func1()
      github.com/shirou/gopsutil/v3@v3.23.8/load/load_windows.go:48 +0x13a
  github.com/shirou/gopsutil/v3/load.loadAvgGoroutine()
      github.com/shirou/gopsutil/v3@v3.23.8/load/load_windows.go:62 +0x338
  github.com/shirou/gopsutil/v3/load.AvgWithContext.func1.1()
      github.com/shirou/gopsutil/v3@v3.23.8/load/load_windows.go:74 +0x44

Previous read at 0x000143310d10 by goroutine 34:
  github.com/shirou/gopsutil/v3/load.AvgWithContext()
      github.com/shirou/gopsutil/v3@v3.23.8/load/load_windows.go:84 +0x1a4
  github.com/shirou/gopsutil/v3/load.Avg()
      github.com/shirou/gopsutil/v3@v3.23.8/load/load_windows.go:69 +0x84
  kitchenette/internal/app/service/sstat.readloadF()
      ts/xxxx.go:30 +0x8a
  kitchenette/internal/app/service/sstat.readload()
      ts/xxxx.go:24 +0xcb

Goroutine 33 (running) created at:
  github.com/shirou/gopsutil/v3/load.AvgWithContext.func1()
      github.com/shirou/gopsutil/v3@v3.23.8/load/load_windows.go:74 +0xa4
  sync.(*Once).doSlow()
      sync/once.go:74 +0xf0
  sync.(*Once).Do()
      sync/once.go:65 +0x44
  github.com/shirou/gopsutil/v3/load.AvgWithContext()
      github.com/shirou/gopsutil/v3@v3.23.8/load/load_windows.go:73 +0xa4
  github.com/shirou/gopsutil/v3/load.Avg()
      github.com/shirou/gopsutil/v3@v3.23.8/load/load_windows.go:69 +0x84
  ts/xxxx.readloadF()
      ts/xxxx.go:30 +0x8a
  ts/xxxx.init.4()
      ts/xxxx.go:16 +0x8a

Goroutine 34 (running) created at:
  ts/xxxx.init.4()
      ts/xxxx.go:17 +0x96
==================

To Reproduce
It will take about 10 minutes to wait.

var statlock sync.RWMutex

func init() {
	readloadF()
	go readload()
}

func readload() {
	loadTicker := time.NewTicker( 5 * time.Second)
	defer loadTicker.Stop()
	for range loadTicker.C {
		readloadF()
	}
}
func readloadF() {
	statlock.Lock()
	defer statlock.Unlock()
	load.Avg()
	load.Misc()
}

Expected behavior
No data race.

Environment (please complete the following information):

  • Windows: Microsoft Windows [Version 10.0.22621.2283]

Additional context

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants