zeromq / cppzmq

Header-only C++ binding for libzmq
http://www.zeromq.org
MIT License
1.94k stars 757 forks source link

Crash when unbind() #517

Closed pi1ot closed 2 years ago

pi1ot commented 2 years ago

I dont know why these code crash at unbind(), comment unbind line will be ok. libzmq v4.3.4 cppzmq v4.8.0 ubuntu 18.04 x86-64

#include "zmq/zmq.hpp"
int main() {
    zmq::context_t ctx = ::zmq::context_t();
    zmq::socket_t socket(ctx, zmq::socket_type::rep);
    socket.bind("tcp://*:7713");
    socket.unbind("tcp://*:7713"); // <- crash here
    socket.close();
}
./zmq_test
terminate called after throwing an instance of 'zmq::error_t'
  what():  No such file or directory
Aborted (core dumped)
Core was generated by `./zmq_test'.
Program terminated with signal SIGABRT, Aborted.
#0  __GI_raise (sig=sig@entry=0x6) at ../sysdeps/unix/sysv/linux/raise.c:51
51      ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
[Current thread is 1 (Thread 0x7f6d08971740 (LWP 10954))]
gdb$ bt
#0  __GI_raise (sig=sig@entry=0x6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1  0x00007f6d07844921 in __GI_abort () at abort.c:79
#2  0x00007f6d07e99957 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00007f6d07e9fae6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4  0x00007f6d07e9fb21 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5  0x00007f6d07e9fd54 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x000055c8b444f15a in zmq::detail::socket_base::bind (addr_=<optimized out>, this=<optimized out>) at zmq.hpp:1815
#7  0x000055c8b444f234 in zmq::detail::socket_base::unbind (addr_=0x55c8b444f5f0 "tcp://*:7713", this=0x7fff658e4190) at zmq_test.cpp:9
#8  main () at zmq_test.cpp:7
gummif commented 2 years ago

This is because an exception is thrown from unbind. This is because the address is wildcard. Try unbinding with the result from

std::string last_endpoint =
        socket.get(zmq::sockopt::last_endpoint);
pi1ot commented 2 years ago

This is because an exception is thrown from unbind. This is because the address is wildcard. Try unbinding with the result from

std::string last_endpoint = socket.get(zmq::sockopt::last_endpoint);

Thanks! unbind success now, but bind and unbind udp address still crash, even unbind sockopt::last_endpoint address

#include "zmq/zmq.hpp"
int main() {
    zmq::context_t ctx = ::zmq::context_t();
    zmq::socket_t socket(ctx, zmq::socket_type::dish);
    socket.bind("udp://224.0.0.130:7714");
    socket.unbind(socket.get(zmq::sockopt::last_endpoint)); // <- still crash here
    socket.close();
}
gummif commented 2 years ago

Try catching exceptions, and try printing what last endpoint is. Maybe this does not work for UDP.

pi1ot commented 2 years ago

Try catching exceptions, and try printing what last endpoint is. Maybe this does not work for UDP.

last endpoint is same as bind address:"224.0.0.130:7714",and exception is "Invalid argument"

gummif commented 2 years ago

This looks like an issue with https://github.com/zeromq/libzmq, try submitting an issue there if this is still a problem.