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

Fix VirtualizationWithContext() race in linux #951

Merged
merged 1 commit into from
Oct 11, 2020

Conversation

rmfitzpatrick
Copy link
Contributor

@rmfitzpatrick rmfitzpatrick commented Oct 6, 2020

The current common linux VirtualizationWithContext() function* isn't thread safe. These changes introduce a cache singleton to prevent data races:

With only test update:

root@1a49945dd9fa:/go/src/github.com/shirou/gopsutil/host# go test -race -run=TestVirtualization
==================
WARNING: DATA RACE
Write at 0x0000007d8548 by goroutine 8:
  github.com/shirou/gopsutil/internal/common.VirtualizationWithContext()
      /go/src/github.com/shirou/gopsutil/internal/common/common_linux.go:243 +0x933
  github.com/shirou/gopsutil/host.VirtualizationWithContext()
      /go/src/github.com/shirou/gopsutil/host/host_linux.go:365 +0x5a
  github.com/shirou/gopsutil/host.Virtualization()
      /go/src/github.com/shirou/gopsutil/host/host.go:141 +0x97
  github.com/shirou/gopsutil/host.TestVirtualization.func1()
      /go/src/github.com/shirou/gopsutil/host/host_test.go:153 +0x44

Previous read at 0x0000007d8548 by goroutine 14:
  github.com/shirou/gopsutil/internal/common.VirtualizationWithContext()
      /go/src/github.com/shirou/gopsutil/internal/common/common_linux.go:117 +0x52
  github.com/shirou/gopsutil/host.VirtualizationWithContext()
      /go/src/github.com/shirou/gopsutil/host/host_linux.go:365 +0x5a
  github.com/shirou/gopsutil/host.Virtualization()
      /go/src/github.com/shirou/gopsutil/host/host.go:141 +0x97
  github.com/shirou/gopsutil/host.TestVirtualization.func1()
      /go/src/github.com/shirou/gopsutil/host/host_test.go:153 +0x44

Goroutine 8 (running) created at:
  github.com/shirou/gopsutil/host.TestVirtualization()
      /go/src/github.com/shirou/gopsutil/host/host_test.go:152 +0xbe
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:1054 +0x1eb

Goroutine 14 (running) created at:
  github.com/shirou/gopsutil/host.TestVirtualization()
      /go/src/github.com/shirou/gopsutil/host/host_test.go:152 +0xbe
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:1054 +0x1eb
==================

With complete diff:

root@1a49945dd9fa:/go/src/github.com/shirou/gopsutil/host# go test -race -run=TestVirtualization
PASS
ok  	github.com/shirou/gopsutil/host	0.044s

bogdandrutu pushed a commit to open-telemetry/opentelemetry-collector that referenced this pull request Oct 6, 2020
This version introduced a race condition:
shirou/gopsutil#951
@shirou
Copy link
Owner

shirou commented Oct 11, 2020

I can reproduce race problem with go test -race -run=TestVirtualization on 1.15.2. Thank you!

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.

2 participants