go run command not providing enough information to debug - go

when I was trying out scenarios on channels in go, I came across a code to reproduce deadlock as below
package main
import (
"fmt"
)
func main() {
c := make(chan bool)
c <- true
}
when I run it using
go run gorouting.go
I am getting the below output
> main.main()
> E:/GO Samples/gorouting.go:13 +0x57
> exit status 2
but when I did run in https://play.golang.org I got more details about the exception, am I missing something in the command or do i need to do any configuration at the machine level?
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan send]:
main.main()
/tmp/sandbox592049259/main.go:7 +0x60
I am running with this configuration
go version go1.10.3 windows/amd64
Thanks for the help

When you use go run xxx.go go only compiles/runs that file within the main package...versus running go install && xxx where xxx is the name of your executable. go install builds everything and copies to the bin dir...try that locally.

Related

cmd.StdoutPipe example in go pkg docs does not run in playground

cmd.StdoutPipe example at go documentation: https://pkg.go.dev/os/exec#example-Cmd.StdoutPipe does not run in playground.
https://play.golang.org/p/ek7-_Xa_bN3
Error:
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [IO wait]:
internal/poll.runtime_pollWait(0x7faec2ac4e88, 0x72)
/usr/local/go-faketime/src/runtime/netpoll.go:234 +0x89
internal/poll.(*pollDesc).wait(0xc000074180, 0xc000100000, 0x1)
/usr/local/go-faketime/src/internal/poll/fd_poll_runtime.go:84 +0x32
internal/poll.(*pollDesc).waitRead(...)
/usr/local/go-faketime/src/internal/poll/fd_poll_runtime.go:89
internal/poll.(*FD).Read(0xc000074180, {0xc000100000, 0x200, 0x200})
/usr/local/go-faketime/src/internal/poll/fd_unix.go:167 +0x25a
os.(*File).read(...)
/usr/local/go-faketime/src/os/file_posix.go:32
os.(*File).Read(0xc00000e028, {0xc000100000, 0x28, 0xc000060e80})
/usr/local/go-faketime/src/os/file.go:119 +0x5e
encoding/json.(*Decoder).refill(0xc00007e000)
/usr/local/go-faketime/src/encoding/json/stream.go:165 +0x17f
encoding/json.(*Decoder).readValue(0xc00007e000)
/usr/local/go-faketime/src/encoding/json/stream.go:140 +0xbb
encoding/json.(*Decoder).Decode(0xc00007e000, {0x4cf580, 0xc00000c048})
/usr/local/go-faketime/src/encoding/json/stream.go:63 +0x78
main.main()
/tmp/sandbox2294589397/prog.go:24 +0x185
Program exited.
Locally it's running correctly, no deadlock. I am not able to understand why deadlock happens in go playground.
From the documentation on the os.exec package (which is where this example comes from):
Note that the examples in this package assume a Unix system. They may
not run on Windows, and they do not run in the Go Playground used by
golang.org and godoc.org.
That note doesn't provide a reason, but the reason is presumably that allowing user-provided unix commands to run would give a broader attack surface for malicious code. It's not that it's impossible to allow this in a relatively secure way, but there's various tradeoffs which make disallowing os.exec a natural choice.

Every "go" command leads to a panic from a certain main.go

I've noticed that every go command has stopped working, due to a panic from a main.go in a particular module:
> go env
panic: required key FOO missing value
goroutine 1 [running]:
github.com/kelseyhightower/envconfig.MustProcess(...)
/Users/kurtpeek/go/pkg/mod/github.com/kelseyhightower/envconfig#v1.4.0/envconfig.go:233
main.main()
/Users/kurtpeek/go/pkg/mod/github.com/myorg/mymodule/go#v0.0.0-20210129234103-92f90e2df5c0/main.go:13 +0x314
where the 'offending' main go is similar to
package main
import (
"github.com/kelseyhightower/envconfig"
"github.pie.apple.com/someorg/somemodule/config"
)
func main() {
cfg := &config.Config{}
envconfig.MustProcess("", cfg)
}
I have no idea why a go env command should fail for this reason?
Your module is called github.com/myorg/mymodule/go, which means that the installed binary is called "go" (after the last path segment). This binary likely shadows the go tool depending on how your PATH is configured.
I suggest you change the module path to avoid this problem.
You might try to reinstall go as it seems that somehow the binary for go env command is replaced by a binary you were potentially trying to compile, possible reason could be you built the program in the directory containing the go tools. I recommend reinstalling go

Code display issue in Delve for Go on Windows 10

I've installed Delve on Windows 10 and it seems to work but the display does not show line numbers and does not point out the current line.
According to the Delve "Getting Started" Guide I should see something like
(dlv) continue
> main.main() ./test.go:5 (hits goroutine(1):1 total:1) (PC: 0x49ecf3)
1: package main
2:
3: import "fmt"
4:
=> 5: func main() {
6: fmt.Println("delve test")
7: }
(dlv)
But what I see is more like
(dlv) continue
> main.main() ./test.go:5 (hits goroutine(1):1 total:1) (PC: 0x49ecf3)
package main
import "fmt"
func main() {
fmt.Println("delve test")
}
(dlv)
I can step through the code and display variable values etc, but the lack of a current line pointer => makes it hard to use.
C:> dlv version
Delve Debugger
Version: 1.3.2
Build: $Id: 569ccbd514fc47c8b4c521b142556867ec5e6917 $
C:> go version
go version go1.12.5 windows/amd64
C:>ver
Microsoft Windows [Version 10.0.18362.418]
C:>chcp
Active code page: 850
Perhaps there is a problem due to MS Windows Command-Prompt console capabilities? Is there a workaround or other solution for this?
If you were using PowerShell, maybe you need to change the background color.
Hope it helps.

Trying to reproduce data race in Golang 1.8.3

I try to reproduce race condition in my app, but go build -race doesnt show any error, even on virtual machine with Linux onboard.
Machine:
Windows: 4 cores
Linux: 2 cores (Ubuntu Xenial x64 via Vagrant and VirtualBox)
go version:
Linux: go version go1.8.3 linux/amd64
Windows: go version go1.8.3 windows/amd64
go env:
Linux: https://pastebin.com/pXURKfj3
Windows: https://pastebin.com/MTdjNnVW
Example 1: https://play.golang.org/p/x-eD6bBrzz
Example 2: https://play.golang.org/p/FSg8P7UP8p
Is my examples has data race? Both of them no according to build with go build -race
If examples above hasnt data race condition, could somebody please send me example where i can see a real data race in Golang, so i can test the -race flag? I also found some examples on the web, some of them doesnt compiling, some of them same shows that there is no data race.
Thank you!
Both of your examples has data race issue. You can find out data race in following ways.
go run -race program.go
go test -race <package-name> or go test -race -run=<testcase-func-name>
go build -race program.go then execute the program
go install -race <package-name> then execute the program
Example 1: data race info
$ go build -race datarace-try1.go
$ ./datarace-try1
8
==================
WARNING: DATA RACE
Read at 0x00c4200761a8 by goroutine 7:
main.main.func1()
/Users/jeeva/go_playground/datarace-try1.go:23 +0x74
Previous write at 0x00c4200761a8 by goroutine 6:
main.main.func1()
/Users/jeeva/go_playground/datarace-try1.go:23 +0x8d
Goroutine 7 (running) created at:
main.main()
/Users/jeeva/go_playground/datarace-try1.go:25 +0xee
Goroutine 6 (running) created at:
main.main()
/Users/jeeva/go_playground/datarace-try1.go:25 +0xee
==================
187410
Found 1 data race(s)
Example 2: data race info
$ go build -race datarace-try2.go
$ ./datarace-try2
==================
WARNING: DATA RACE
Read at 0x00c420078178 by main goroutine:
main.main()
/Users/jeeva/go_playground/datarace-try2.go:10 +0x12e
Previous write at 0x00c420078178 by goroutine 6:
main.main.func1()
/Users/jeeva/go_playground/datarace-try2.go:15 +0xd4
Goroutine 6 (finished) created at:
main.main()
/Users/jeeva/go_playground/datarace-try2.go:17 +0xf1
==================
==================
WARNING: DATA RACE
Read at 0x00c420078178 by goroutine 7:
main.main.func2()
/Users/jeeva/go_playground/datarace-try2.go:20 +0x3f
Previous write at 0x00c420078178 by goroutine 6:
main.main.func1()
/Users/jeeva/go_playground/datarace-try2.go:15 +0xd4
Goroutine 7 (running) created at:
main.main()
/Users/jeeva/go_playground/datarace-try2.go:23 +0x11d
Goroutine 6 (finished) created at:
main.main()
/Users/jeeva/go_playground/datarace-try2.go:17 +0xf1
==================
finish
Found 2 data race(s)
exit status 66

Go runtime crash

My go code errors out with a strack trace like this:
unexpected fault address 0x0
fatal error: fault
[signal 0xb code=0x80 addr=0x0 pc=0x407d50]
goroutine 52246872 [running]:
runtime.throw(0xad6a77)
/usr/local/go/src/pkg/runtime/panic.c:464 +0x69 fp=0xc214d2c1f8
runtime.sigpanic()
/usr/local/go/src/pkg/runtime/os_linux.c:237 +0xe9 fp=0xc214d2c210
hash_lookup(0x671ec0, 0xc21001eed0, 0xc214d2c2d0)
/usr/local/go/src/pkg/runtime/hashmap.c:502 +0x150 fp=0xc214d2c290
runtime.mapaccess(0x671ec0, 0xc21001eed0, 0xc214d2c318, 0xc214d2c328, 0xc214d2c330)
/usr/local/go/src/pkg/runtime/hashmap.c:1004 +0x57 fp=0xc214d2c2c0
runtime.mapaccess2(0x671ec0, 0xc21001eed0, 0x389c, 0x2060009ae, 0xad4dc0, ...)
/usr/local/go/src/pkg/runtime/hashmap.c:1061 +0x5d fp=0xc214d2c308
growmobile.com/rtb/cache.HistogramCheck(0xc2121eafd0, 0xc, 0xeca93c6a0, 0x852a9f8, 0xad4dc0, ...)
/mnt/rtb_server/go/src/growmobile.com/rtb/cache/histogram_cache.go:211 +0x16b fp=0xc214d2c798
growmobile.com/rtb/controller.processBidRequest(0xc2121eafd0, 0xc, 0xeca93c6a0, 0x852a9f8, 0xad4dc0, ...)
/mnt/rtb_server/go/src/growmobile.com/rtb/controller/bidder_logic.go:272 +0x7c2 fp=0xc214d2e928
----- stack segment boundary -----
growmobile.com/rtb/controller.Bid(0x7f7a14250ed8, 0xc2197b3a00, 0xc2157f9dd0)
/mnt/rtb_server/go/src/growmobile.com/rtb/controller/controller.go:96 +0x903 fp=0x7f79f8dd9da0
net/http.HandlerFunc.ServeHTTP(0x800850, 0x7f7a14250ed8, 0xc2197b3a00, 0xc2157f9dd0)
/usr/local/go/src/pkg/net/http/server.go:1220 +0x40 fp=0x7f79f8dd9dc0
net/http.(*ServeMux).ServeHTTP(0xc21001e660, 0x7f7a14250ed8, 0xc2197b3a00, 0xc2157f9dd0)
/usr/local/go/src/pkg/net/http/server.go:1496 +0x163 fp=0x7f79f8dd9df8
net/http.serverHandler.ServeHTTP(0xc21001fa00, 0x7f7a14250ed8, 0xc2197b3a00, 0xc2157f9dd0)
/usr/local/go/src/pkg/net/http/server.go:1597 +0x16e fp=0x7f79f8dd9e38
net/http.(*conn).serve(0xc215d60a00)
/usr/local/go/src/pkg/net/http/server.go:1167 +0x7b7 fp=0x7f79f8dd9f98
runtime.goexit()
/usr/local/go/src/pkg/runtime/proc.c:1394 fp=0x7f79f8dd9fa0
created by net/http.(*Server).Serve
/usr/local/go/src/pkg/net/http/server.go:1644 +0x28b
goroutine 1 [chan receive]:
main.serve()
/mnt/rtb_server/go/src/growmobile.com/rtb/rtb_server/rtb_server.go:71 +0x5f0
main.main()
/mnt/rtb_server/go/src/growmobile.com/rtb/rtb_server/rtb_server.go:86 +0x1d8
At the time it is trying to access a hash which is a struct that maps to a pointer. The hash is a global one and looks like this:
var histogramCache = make(map[model.HistogramKey]*model.HistogramValue)
The access is a very standard access which looks like:
value, ok := histogramCache[key]
I tried to google something similar but could not find it. I do not use cgo or assertions.
This is the go version we are using: go version go1.2 linux/amd64
Thank you, any help would be appreciated.
Maps are not safe for concurrent access. This might very well be the reason why your code is failing.
Use a central goroutine (see this example) and retrieve things using channels or use locking tools from sync.
Reinstalling the goland package fixed this problem for me. Some dependencies had been replaced by another package or were not correctly installed.

Resources