-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
runtime: deadlock at _cgo_wait_runtime_init_done #42190
Comments
Is the code you are showing us the complete test case? How exactly are you building and running the program? Please try to show us exactly how we can reproduce the problem ourselves. Thanks. |
The build command and export.go and client.cpp are exactly the same as I posted. |
Test.zip. |
Thanks, but, sorry, I can't use a Visual Studio project. I'm not a Windows user at all. Is there a way that you can show me the exact clang commands that you use to build the executable? Thanks. |
The command in windows: It looks like the problem is windows-only, buildmode=c-archive only. I have tried with buildmode=c-shared and it is ok |
Same issue here, works fine with c-shared but with c-archive _cgo_wait_runtime_init_done just hand due to an infinite loop 'cause runtime_init_done is never 1 and thus the event won't be triggered, still trying to understand the root cause. |
same issue with c-shared in linux. I've compiled a so file to use LD_PRELOAD. Deadlock occurs when using the LD_PRELOAD so file
When I use gdb to debug this, it's stack show:
go version
It works fine in fedora 32 and centos 6, but hangs in ubuntu 1804 and centos 7(3.10.0-862.11.6.el7.x86_64.debug ) |
I've found some clues. I put some debug code in /usr/lib/golang/src/runtime/cgo/gcc_libinit.c to trace the x_cgo_notify_runtime_init_done and _cgo_wait_runtime_init_done.
Here is the log
It's clear that the notify and wait action occur in two different processes(parent process and child process). So deadlock is unavoidable. |
@james-li Using |
@ianlancetaylor Thanks. I post another issue |
Hi, I am also facing this issue on Windows. Tested with go1.12 as well as go1.16. Below are the details of my application scenario. I have generated a c-shared library(say gocshared.dll) from go code using buildmode=c-shared. The module hierarchy is as below: When I call exported function of gocshared.dll, the call never returns. The application dump after this call shows below entries in call stack: 00000000 Note: LegacyApp(Executable), legacy.dll and interface.dll are developed using C++. When I use this shared library using test app, in below shown hierarchy, it works as expected i.e. the call does not hang. Any idea what is wrong? |
I don't know what is wrong. What is supposed to happen is that the Perhaps simply using a |
Thanks for response @ianlancetaylor. I tried debugging test app with windbg to see x_cgo_notify_runtime_init_done call but as there is no pdb generated for gocshared.dll, debugger does not find symbols inside shared library. Is there a way I can debug this on Windows? |
I'm sorry, I don't know the answers to your questions. |
Same issue here. VC++ and a static library (
#include <iostream>
extern "C" int AddFromGo(int a, int b);
int main()
{
std::cout << "Hello World!\n" << AddFromGo(1, 2) << "\n";
}
extern "C" void __mingw_vfprintf() {}
package main
import "C"
//export AddFromGo
func AddFromGo(a, b C.int) C.int {
return a + b
}
func main() {
}
|
Is this related to #35006? |
Calling |
In
My question is whether this is true with the VC++ runtime. |
/CC @thanm |
Hi, I read through this issue. My guess would be that this problem is not related to #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 Thanks, Than |
Thanks. Is this duplicated with #30347? |
Hi, Same issue here. bind go functions for iOS application don't work. but it is ok in simple demo project。 |
CRT initialization can be used to invoke the #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; } |
|
This works great for 64 bit. How about 32 bit? I tried replacing _rt0_amd64_windows_lib with _rt0_386_windows_lib such as #ifdef _MSC_VER
#ifdef __cplusplus but that gets an unresolved external symbol for _rt0_386_windows_lib |
Thanks @ks75vl. I have successfully built both static and shared libraries with MSVC. These are build commands: |
It's about the name mangling, here is the patch version for both 32 and 64 bits. #ifdef _MSC_VER
#ifdef __cplusplus
extern "C" {
#endif
__pragma(section(".CRT$XCU", read));
#ifdef _WIN64
void _rt0_amd64_windows_lib();
__declspec(allocate(".CRT$XCU")) void (*init1)() = _rt0_amd64_windows_lib;
__pragma(comment(linker, "/include:init1"));
#else
void rt0_386_windows_lib();
__declspec(allocate(".CRT$XCU")) void (*init1)() = rt0_386_windows_lib;
__pragma(comment(linker, "/include:_init1"));
#endif
#ifdef __cplusplus
}
#endif
#endif
int main() { return 0; } |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat 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
#include "libcrawler.h"
#include
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.
The text was updated successfully, but these errors were encountered: