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 go race detector races #967

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

cwmos
Copy link

@cwmos cwmos commented Apr 9, 2024

This fixes an issue I had where the Go race detector reported races.

Even though there technically is a race, the existing code probably works fine. However, it is nice to be able to use the Go race detector without having races reported.

I considered using sync.Once instead. However, I wanted to preserve the existing behavior, that in case of problems reading "/proc/net/psched", we try to read it again next time.

@cwmos cwmos force-pushed the fix-go-race-detector-races branch from 004c780 to f4b6d98 Compare April 9, 2024 14:11
@cwmos cwmos force-pushed the fix-go-race-detector-races branch from f4b6d98 to 5707c43 Compare April 10, 2024 06:13
@cwmos
Copy link
Author

cwmos commented Apr 18, 2024

Someone may have concerns about using a mutex for this for efficiency reasons?

I think it should be very efficient as in most cases it will just use CompareAndSwapInt32 - see https://cs.opensource.google/go/go/+/refs/tags/go1.22.2:src/sync/mutex.go;l=81

I could instead put all the values into a struct and store the struct or a pointer to the struct in an atomic.Value. Perhaps that would be a bit nicer?

Or use the typesafe atomic.Pointer instead, but that requires go 1.19.

Alternatively I could make a solution using math.Float64bits, math.Float64frombits, atomic.LoadUint64 and atomic.StoreUint64. But that seems a bit cumbersome..

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.

None yet

1 participant