-
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
all: race condition in getting environment variable on windows #9753
Comments
Can you demonstrate the bug with a bit of code? I really don't see what the problem is. Thank you. Alex |
Consider another thread calling SetEnvironmentVariable concurrently with this code.
On Feb 2, 2015, at 10:43 PM, alexbrainman notifications@github.com wrote:
|
Same bug here: Consider another thread calling SetCurrentDirectory.
On Feb 3, 2015, at 12:07 AM, Jay jay.krell@cornell.edu wrote:
|
package main
import (
"os"
"runtime"
"time"
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
go func() {
for {
os.Setenv("FOO", "GO")
os.Setenv("FOO", "GOLAAAAAAAAAAAAAAAAAAAAAANG")
}
}()
time.Sleep(1e9)
for {
e := os.Getenv("FOO")
if e != "GO" && e != "GOLAAAAAAAAAAAAAAAAAAAAAANG" {
panic("BUG!: " + e)
}
}
} Hmm, I run this code but couldn't repro |
package main
import (
"fmt"
"os"
"runtime"
"strings"
"time"
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
n := 1
go func() {
for {
os.Setenv("FOO", "GOL"+strings.Repeat("O", n)+"NG")
if n++; n > 5000 {
n = 1
}
}
}()
time.Sleep(1e9)
for {
s := os.Getenv("FOO")
if !strings.HasSuffix(s, "ONG") {
panic(fmt.Sprintf("BUG(%d): %s", n, s))
}
}
} http://play.golang.org/p/srvhu31O1r I could make this crash on windows easily... 👎
GetEnvironmentVariable is not thread-safe!? |
I see. I agree we should fix this. Thank you. Alex |
It is thread-safe. But there is no public lock to be had around multiple calls to it. This a common aspect of various "Get" functions. GetFullPathame, GetModuleFileName, etc.
On Feb 3, 2015, at 12:33 AM, mattn notifications@github.com wrote:
|
I looked in main repo. These are to be fixed: net/getAdapterList (@jaykrell can you see others?) None of them look likely to happens in real program (IMO), but we might as well fix them. Once fixed we should copy syscall changes into golang.org/x/sys/windows. @jaykrell interested to make changes? If not I will do it myself. Alex |
Here https://go-review.googlesource.com/4940 is the fix. Alex |
CL https://golang.org/cl/11088 mentions this issue. |
https://github.com/golang/sys/blob/master/windows/env_windows.go
This code exhibits a common bug.
Other threads can change the value, or unset it, while it is being retrieved.
You should loop until either
not found
or variable fits in buffer
The text was updated successfully, but these errors were encountered: