title: zmq_ctx_term() blocks while sockets closed
body: <p>I am working with ZMQ and am running into issues when destroying a context.</p>
  
  <p>I have the code</p>
  
  <pre><code>zmq_ctx_shutdown(context);
  zmq_ctx_term(context);
  </code></pre>
  
  <p>This code always blocks on <code>zmq_ctx_term()</code> and will also block with <code>zmq_ctx_destoy()</code>; however, if that call is removed and only the shutdown call is used, everything seems to work fine, but the application will leak memory from not freeing the zmq context.</p>
  
  <p>In my code, I have four sockets: two <code>ZMQ_PAIR</code> <code>inproc</code> sockets that are used to communicate between the main thread and a thread with a <code>ZMQ_REP</code> socket, along with a <code>ZMQ_PUB</code> that runs on the main thread.</p>
  
  <p>I have set <code>ZMQ_LINGER</code> on all of these sockets to 0, as it seems the only time the <code>zmq_ctx_term()</code> call should block is if messages have not yet been sent since I have called <code>zmq_close()</code> on all sockets, which returns without errors. Additionally, this blocking still occurs if I only call <code>zmq_ctx_new()</code>, create the sockets, then call <code>zmq_ctx_shutdown(); zmq_ctx_term();</code>.</p>
  
  <p>My fear is that something is wrong with using the two <code>inproc</code> sockets to communicate between threads with the same zmq context, although there doesn't seem to be any issues with the communication as messages are being received.</p>
  
  <p>After fearing that I may have some issue with thread safety, I found that the ZMQ context should be thread-safe, but not the sockets. I have checked and in the main thread, I am opening an closing the <code>ZMQ_PUB</code> socket and <code>inproc</code> socket. In another thread, I am opening and closing the <code>ZMQ_REP</code> socket and other end of the <code>inproc</code> socket, so it seems like that shouldn't be an issue.</p>
  
  <p>For clarification, I am writing my code in C using libzmq from the current master branch on GitHub (commit d35473e). I did see a similar on issues when linking ZMQ's shared library, although this is occurring whether I use the static or shared library. I am currently on OS X 10.9.</p>
  
  <p>If anyone has time to look at the code as a whole, the relevant file is listed <a href="http://pastebin.com/SstySqza" rel="nofollow">here</a>.</p>
  
  <p>Any idea of what is going on here?</p>