Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go programs hanging on Windows 10

Tags:

go

windows-10

I've been having issues with all Go programs hanging on Windows, even a simple Hello world program. I've reinstalled Golang multiple times with nothing changing. I can't use Ctrl+X or Ctrl+D to close the program either. It'll still spawn the process which will show up on Process Monitor. But I can't kill it from there or from the command line.

There was a similar issue to this posted on reddit https://www.reddit.com/r/golang/comments/2lvnqk/not_even_hello_world_works/. But that still has no resolution.

The is the program I'm trying to get to run right now:

package main

import "fmt"

func main() {
    fmt.Println("Hello, world!")
}

Running it with go run -x gives this output:

WORK=C:\Users\User\AppData\Local\Temp\go-build882050150
mkdir -p $WORK\command-line-arguments\_obj\
mkdir -p $WORK\command-line-arguments\_obj\exe\
cd E:\go\src\github.com\test\hello
"C:\\Go\\pkg\\tool\\windows_amd64\\compile.exe" -o "C:\\Users\\User\\AppData\\Local\\Temp\\go-build882050150\\command-line-arguments.a" -trimpath "C:\\Users\\User\\AppData\\Local\\Temp\\go-build882050150" -p main -complete -buildid 0180ed6e175ea3e4bc497fc21fe0319a733a9c8e -D _/E_/go/src/github.com/test/hello -I "C:\\Users\\User\\AppData\\Local\\Temp\\go-build882050150" -pack "E:\\go\\src\\github.com\\test\\hello\\main.go"
cd .
"C:\\Go\\pkg\\tool\\windows_amd64\\link.exe" -o "C:\\Users\\User\\AppData\\Local\\Temp\\go-build882050150\\command-line-arguments\\_obj\\exe\\main.exe" -L "C:\\Users\\User\\AppData\\Local\\Temp\\go-build882050150" -w -extld=gcc -buildmode=exe -buildid=0180ed6e175ea3e4bc497fc21fe0319a733a9c8e "C:\\Users\\User\\AppData\\Local\\Temp\\go-build882050150\\command-line-arguments.a"
$WORK\command-line-arguments\_obj\exe\main.exe

After that it just sits there and dose nothing. Doesn't matter what program I run it will always hang forever.

No idea where to go from here. I don't really develop on Windows but I was trying to work with cross-compilation on Ubuntu and having run into this issue with the cross-compiled binary I thought I'd try compiling directly on Windows. But it appears my Windows just doesn't like Golang compiled binaries.

Go version is 1.5.1 windows/amd64.

Here's the output of go env:

set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=E:\go
set GORACE=
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GO15VENDOREXPERIMENT=
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1
like image 322
JDWardle Avatar asked Dec 02 '15 02:12

JDWardle


People also ask

Why does Windows 10 hang so much?

Random hangs & freezes on Windows 10 sometimes happens when system files get corrupted. To test and correct it, run a Command Prompt as administrator, in the black Command Prompt window, type: sfc /scannow. Then press Enter on your keyboard.

Why do my programs keep freezing?

Overheating, faulty software, faulty hardware, and issues with your memory are just a few of the potential causes of chronic freezes. If your computer only freezes once, it probably isn't anything concerning.


2 Answers

Avast was being dumb. Disabled Avast and the programs all worked....

Of course leaving Avast disabled is not ideal so I had to figure out a way to exclude go files. Unfortunately there's no way to exclude go compiled files themselves, unless you set up individual exclusions for the binaries. However excluding go specific directories will work.

I ended up excluding the installation location of go for me this was C:\Go and the directory of GOPATH E:\go for me.

This worked for the most part as long as binaries were run from either of those two directories, but I was still having issues with using the go run command. Using the go run -x command I was able to find out where Go was putting the temporary compiled binaries. This ended up being at C:\Users\username\AppData\Local\Temp\go-build1234\. However go would create a new directory each time I used go run so I had to create an exclusion on Avast with two wildcards set C:\Users\username\AppData\Local\Temp\go-build*\*. Now programs will run correctly.

These are the exclusions I ended up with: Go Avast exclusions

like image 181
JDWardle Avatar answered Sep 18 '22 10:09

JDWardle


The problem is that if GOTMPDIR is not set, golang will sometimes build in that generic \Temp directory that is dangerous to allow as a generic exception to Kaspersky. And for me, Kaspersky doesn't allow wildcards in the directory exceptions like Avast

Assuming you've already added your GOPATH as an exception in your antivirus (for example, "C:\Users<user>\go"...what worked for me was to

  1. add a new directory (I named mine "tmp" but you could probably name it a number of things) within that path, i.e., "C:\Users<user>\go\tmp"
  2. Set the golang environment variable GOTMPDIR to it...

go env -w GOTMPDIR="C:\Users<user>\go\tmp"

And the issue goes away. But note that I already added "C:\Users<user>\go" as an exception to my antivirus.

like image 44
Gronkey Kong Jr Avatar answered Sep 22 '22 10:09

Gronkey Kong Jr