-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
cmd/compile: incorrect optimizations. #30339
Comments
Hi, your code has a data race on |
yeah, it's in data race. but if I init finish with |
A program with a data race is an invalid program, and it can behave in unexpected ways. You should just fix the data race. If you think you've found a compiler bug, file an issue with a reproducer that has no data races. |
Since the compiler is allowed to assume that the code does not contain any data race (because if it does, it's not valid code), it's possible that it'll perform optimizations that don't really make sense (and completely break) for racy code. To sum it up: don't look at the generated code for racy programs and expect it to make sense, because there's no guarantee that it'll do. |
I think that a perhaps simpler way to describe the problem is to observe that this loop for !finish {
finish = gDone
} has no synchronization. Therefore nothing will ever cause this goroutine to observe that You seem to be making the assumption that the program should check the value of That is, I wouldn't say that the real problem is that your program is racy (although it is). The real problem is that you are assuming that one goroutine will see changes made by another goroutine without synchronization. That assumption is not valid in Go. For more details see https://golang.org/ref/mem. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
i write an example code as below and run with go run main.go
What did you expect to see?
should output below and exit.
What did you see instead?
only output and never exit
when i run this code with go run main.go,it's hang in the for loop and never exit.
instead,i run with go run -gcflags "-N -l" main.go it work ok as what i want to see.
compile this code into assemble with optimizations
we can see that the for loop is optimize into below. the assign code
finish=gDone
is optizime so this loop is always true and never exit.The text was updated successfully, but these errors were encountered: