pebbe / zmq4

A Go interface to ZeroMQ version 4
BSD 2-Clause "Simplified" License
1.17k stars 163 forks source link

Static linked zmq with lpthread causes segfault #130

Closed omani closed 6 years ago

omani commented 6 years ago

Hi pebbe,

I don't know if you already know but linking like this:

CGO_CPPFLAGS="-I/usr/include" CGO_LDFLAGS="-L/usr/lib -L/usr/lib/x86_64-linux-gnu -lzmq -lpthread -lsodium -lrt -lstdc++ -lm -lc -lgcc" go build -v --ldflags '-extldflags "-static"' -a main.go

results in a segmentation fault (core dumped) message when running the binary, because of this issue: https://sourceware.org/bugzilla/show_bug.cgi?id=10652

can you suggest a way for how to dynamically link pthread into my application?

or any other way to make things just work.

omani commented 6 years ago

the above link states:

The getaddrinfo call causes an internal segmentation fault when called from threads and the binary is linked with "-static". The documentation says the function is thread safe. This should be also the case when linked with "-static" since there is no exception mentioned. The crash only occurs if the binary is executed on a multi core system, on a single core system it does not crash. This seems to be a synchronization problem inside the library, but somehow only in the static version.

omani commented 6 years ago

according to https://github.com/zeromq/libzmq/issues/2849 I cannot statically link to glibc - it uses runtime plugins for things like DNS resolution.

so does this mean that a static linked zmq application is not possible for now even if I am able to actually build the binary successfully?

pebbe commented 6 years ago

Why do you need all those settings? It should work without. Just try this:

   go build main.go

If it can't find the C-library of ZeroMQ because it is in a non-default location, you can try reinstalling zmq4 like this:

ZMQ=/path/to/zeromq
PKG_CONFIG_PATH=$ZMQ/lib/pkgconfig CGO_LDFLAGS="-Wl,-rpath=$ZMQ/lib" go get github.com/pebbe/zmq4
omani commented 6 years ago

Why do you need all those settings? It should work without.

well I need a binary which comes with zmq built in statically. so I don't need libzmq on the target machine.

omani commented 6 years ago

I have to say distributing an application which uses libzmq/zmq4 as a binary is really hard.

you either just say:

go build main.go

and hope your customers have the right zmq version, else they get:

zmq4 was installed with ZeroMQ version XXX, but the application links with version XXX

or you statically link libzmq when building with the command given in the opening post, but then you segfault.

ideally I want to build a binary which has everything in it, so you can run it on any linux distribution without any dependencies (like installing libzmq3-dev).

It is a dillema. how do you guys deal with all this?

omani commented 6 years ago

83 helped me here.

closing.