go: runtime: deadlock at _cgo_wait_runtime_init_done

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

$ go version
go version go1.15.3 windows/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\nguyenha\AppData\Local\go-build
set GOENV=C:\Users\nguyenha\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\nguyenha\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\nguyenha\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=c:\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=D:\work\projects\mimic\mimic-core\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\nguyenha\AppData\Local\Temp\go-build126925810=/tmp/go-build -gno-record-gcc-switches

What did you do?

set CGO_ENABLED=1 set CC=clang go build -buildmode=c-archive -o bin/libcrawler.lib -v core/export

Go side: package main import “C”

//export Init func Init() { print(“something”) } // Required by CGO // func main() {

}

From C++ side: #include <iostream> #include “libcrawler.h” #include <cstring>

using namespace std;

int main() { cout << “Hello, world1!” << endl;

// x_cgo_notify_runtime_init_done(NULL); Init(); cout << “Hello, world!” << endl; return 0; }

I use clang-cl to compile c++ project

What did you expect to see?

Can call Go function (Init()) from C++

What did you see instead?

A deadlock at _cgo_wait_runtime_init_done. I tried to test by calling x_cgo_notify_runtime_init_done(NULL); then I could pass the loop but crash later.

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 27 (12 by maintainers)

Most upvoted comments

Thanks @ks75vl. I have successfully built both static and shared libraries with MSVC.

These are build commands: https://github.com/jat001/ddns4cdn?tab=readme-ov-file#msvc https://github.com/jat001/ddns4cdn?tab=readme-ov-file#msvc-1

CRT initialization can be used to invoke the _rt0_amd64_windows_lib, which can remove deadlocks in _cgo_wait_runtime_init_done

#ifdef _MSC_VER
#ifdef __cplusplus
extern "C" {
#endif

	void _rt0_amd64_windows_lib();

	__pragma(section(".CRT$XCU", read));
	__declspec(allocate(".CRT$XCU")) void (*init1)() = _rt0_amd64_windows_lib;
	__pragma(comment(linker, "/include:init1"));

#ifdef __cplusplus
}
#endif
#endif

int main() { return 0; }

Hi,

I read through this issue.

My guess would be that this problem is not related to https://github.com/golang/go/issues/35006. That issue (and the various CLs I submitted to the linker) has to do mainly with cgo internal linking to help with programs that use the race detector (“go build -race”) in combination with contemporary versions of GCC and clang.

Given what you mention in this comment and what Ian said previously, I think the problem has to do with the .ctors section, and why this mechanism is not working properly in this case.

Thanks, Than