golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.97k stars 17.67k forks source link

runtime: cgo Unix C code can't modify environment variables for Go #27693

Open rmmh opened 6 years ago

rmmh commented 6 years ago

What version of Go are you using (go version)?

1.11

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

linux amd64

What did you do?

Because of envOnce and copyenv in env_unix.go, linked non-Go programs can't set environment variables that are visible to Go code.

This is probably WAI, but it's surprising when embedding and could use a mention somewhere.

package main

/*
#include <stdlib.h>       // for setenv()
*/
import "C"

import (
    "fmt"
    "os"
)

func main() {
    os.Setenv("foo", "1")
    fmt.Println("before:", os.Getenv("foo"))
    C.setenv(C.CString("foo"), C.CString("2"), 1)
    fmt.Println("after:", os.Getenv("foo"))
    os.Setenv("foo", "3")
    fmt.Println("after Go:", os.Getenv("foo"))
}

What did you see instead?

"1 1 3" instead of "1 2 3".

ianlancetaylor commented 6 years ago

Any suggestions on where this could be documented where the people who need to know about it would actually see it?

rmmh commented 6 years ago

Maybe a "Known Issues" section on the cmd/cgo docs might work, linking to a relevant GitHub search? It already has some tidbits like "avoid passing uninitialized C memory to Go code if the Go code is going to store pointer values in it. Zero out the memory in C before passing it to Go.". I'm sure there are a few other cgo gotchas that should be documented.

davecheney commented 6 years ago

Even if this was possible, I don’t see how it could be data race free when C code is mixed with goroutines.

On 15 Sep 2018, at 11:03, Ryan Hitchman notifications@github.com wrote:

Maybe a "Known Issues" section on the cmd/cgo docs might work, maybe linking to a relevant GitHub search? It already has some tidbits like "avoid passing uninitialized C memory to Go code if the Go code is going to store pointer values in it. Zero out the memory in C before passing it to Go.". I'm sure there are a few other cgo gotchas that should be documented.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

dmitshur commented 6 years ago

This issue has a high similarity to #22302 in nature, which was resolved with CL 75751. Perhaps that is a good example to follow?

It stands out that unlike runtime.GOROOT being a single function, there are many functions in os that deal with getting/setting environment variables.

wsc1 commented 6 years ago

I have added it to the wiki

For me, that would suffice.