Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZMQ recv() is blocking even after the context was terminated

I did my best to follow the instructions in the ZMQ termination whitepaper, but so far I'm failing miserably. I have a parent class, which spawns a listener thread (using win32-pthreads).

Accoring to the whitepaper, when terminating, I should set the _stopped flag, delete the context, which in turn would call zmq_term() and release the blocking recv(). Instead, what I get is either:

  • calling delete _zmqContext crashes the application (probably with a segmentation fault)
  • replacing the delete with zmq_term(_zmqContext) does not release the blocking recv()

I'm adding a partial code sample, which is long because I'm not sure which part may be important.

AsyncZmqListener.hpp:

class AsyncZmqListener
{
public:
    AsyncZmqListener(const std::string uri);
    ~AsyncZmqListener();

    bool Start();
    void Stop();

private:
    static void* _threadEntryFunc(void* _this);     
    void _messageLoop();

private:
    bool _stopped;
    pthread_t _thread;
    zmq::context_t* _zmqContext;
};

AsyncZmqListener.cpp:

AsyncZmqListener::AsyncZmqListener(const std::string uri) : _uri(uri)
{
    _zmqContext = new zmq::context_t(1);
    _stopped = false;
}

void AsyncZmqListener::Start()
{
    int status = pthread_create(&_thread, NULL, _threadEntryFunc, this);
}

void AsyncZmqListener::Stop()
{
    _stopped = true;
    delete _zmqContext;             // <-- Crashes the application. Changing to 'zmq_term(_zmqContext)' does not terminate recv()
    pthread_join(_thread, NULL);    // <-- This waits forever
}

void AsyncZmqListener::_messageLoop()
{        
    zmq::socket_t listener(*_zmqContext, ZMQ_PULL);
    listener.bind(_uri.c_str());

    zmq::message_t message;    
    while(!_stopped)
    {
        listener.recv(&message);    // <-- blocks forever
        process(message);
    }
}

P.S.

I'm aware of this related question, but none of the answers quite match the clean exit flow described in the whitepaper. I will resolve to polling if I have to...

like image 659
bavaza Avatar asked Sep 15 '13 10:09

bavaza


1 Answers

ZMQ recv() did unblock after its related context was terminated

I was not aware that recv() throws an ETERM exception when this happens. Revised code that works:

void AsyncZmqListener::_messageLoop()
{        
    zmq::socket_t listener(*_zmqContext, ZMQ_PULL);
    listener.bind(_uri.c_str());

    zmq::message_t message;    
    while(!_stopped)
    {
        try
        {
            listener.recv(&message);
            process(message);
        }
        catch(const zmq::error_t& ex)
        {
            // recv() throws ETERM when the zmq context is destroyed,
            //  as when AsyncZmqListener::Stop() is called
            if(ex.num() != ETERM)
                throw;
        }
    }
}
like image 50
bavaza Avatar answered Sep 28 '22 08:09

bavaza