zeromq / zmqpp

0mq 'highlevel' C++ bindings
http://zeromq.github.io/zmqpp
Mozilla Public License 2.0
439 stars 195 forks source link

How to actually handle a graceful close? #254

Open Gandalf1783 opened 7 months ago

Gandalf1783 commented 7 months ago

I have a while-loop which is receiving via socket->receive(message);.

I call another function inside the same class (but from a different thread) which tells the while loop to end. Additonally, I have to close the sockets so the term/destroy on the context is not erroring out.

After both socket operations, I just get "Ressource temporarily unavailable". Why is that? The sockets gets bound to the same endpoint, but cannot be closed?

Of course, in the other Thread its still running socket->receive(message), but that one should stop once I call zmq_ctx_term() according to the docs.

TLDR;: How do I actually close zmqpp gracefully from another thread?

Gandalf1783 commented 7 months ago

This is the current code that I'm using to try to close the socket gracefully:

 printf("Closing Socket: \n");
    std::string endpoint = this->socket->get<std::string>(zmqpp::socket_option::last_endpoint);

    std::cout << "Unbinding: " << endpoint << std::endl;

    this->socket->unbind(endpoint);
    printf("ERRNO: %s\n", zmq_strerror(zmq_errno()));

    zmq_close(*this->socket.get());
    printf("ERRNO: %s\n", zmq_strerror(zmq_errno()));    

    printf("Destroy Context: \n");

    zmq_ctx_term(*(this->context.get()));
    printf("ERRNO: %s\n", zmq_strerror(zmq_errno()));

I understand that also socket->term(); exists, but it doesnt work either way.

The current output to the code above is:

Closing Socket: 
Unbinding: tcp://127.0.0.1:5555
ERRNO: Resource temporarily unavailable
ERRNO: Resource temporarily unavailable
Destroy Context: 
terminate called after throwing an instance of 'zmqpp::zmq_internal_exception'
  what():  Context was terminated
[2]    98074 IOT instruction (core dumped)

This does seem like the way to go, but it isnt working well....