Got this figured out, so I thought I&#39;d update the list:<div><br></div><div>The problem was a call to `addFinalizer` on a `Socket` value, where</div><div><br></div><div><div>data Socket a = Socket {</div><div>      _socket   :: Ptr ()</div>

<div>    , _sockLive :: IORef Bool</div><div>    }</div><div><br></div><div>Under 7.4.1 with -threaded, the finalizer was being run prematurely. I&#39;m not clear on why, but the notes here [1] would suggest it&#39;s because the Socket type is optimized away. In any case, putting the finalizer on the inner Ptr () seems to have fixed the problem.</div>

<div><br clear="all">Mike Craig<br><br>
<br><br><div class="gmail_quote">On Wed, Feb 15, 2012 at 7:02 AM, Michael Craig <span dir="ltr">&lt;<a href="mailto:mkscrg@gmail.com" target="_blank">mkscrg@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


Ok, so I&#39;ve found that using the -V0 RTS flag to turn off the RTS clock (and associated signals) makes the code run fine with -threaded. I&#39;m still digging through the implications of that, though. Has some behavior here changed in 7.4.1?<div>



<br clear="all">Mike Craig<div><div><br><br>
<br><br><div class="gmail_quote">On Tue, Feb 14, 2012 at 3:54 PM, Michael Craig <span dir="ltr">&lt;<a href="mailto:mkscrg@gmail.com" target="_blank">mkscrg@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">



Hi all,<div><br></div><div>I&#39;m debugging an issue with multithreading and FFI calls in 7.4.1. The code in question is the zeromq3-haskell library, which provides an FFI binding to ZeroMQ.</div><div><br></div><div>A little background: ZeroMQ gives the programmer &quot;contexts&quot; and &quot;sockets&quot;. Contexts are thread-safe and generally used one-per-process, but sockets and not thread-safe so they&#39;re generally used one-per-thread.</div>




<div><br></div><div>Have a look at the code in this gist: <a href="https://gist.github.com/1829190" target="_blank">https://gist.github.com/1829190</a> It&#39;s just a server that prints requests and replies with &quot;Hello&quot;, and a client that sends the requests [&quot;1&quot;, &quot;2&quot;, ..., &quot;10&quot;] and prints the responses.</div>




<div><br></div><div>Despite the fact that ZMQ sockets are not thread-safe, it was possible to compile and run this code with -threaded under 7.0.4. Using MVars or some other locking mechanism, it was even possible to use the same socket from multiple parallel threads. (As long as the socket was not actually used in parallel by multiple threads, all was well.) In 7.4.1, the code will only run if compiled without -threaded. With -threaded, it croaks with an &quot;operation on non-socket&quot; error, which generally refers to a socket that&#39;s either been closed or </div>




<div><br></div><div>I&#39;ve dug through the code in zeromq3-haskell and haven&#39;t found anything suspicious looking. Before I go digging into libzmq itself, can somebody assure me that nothing has changed in 7.4.1 with regard to -threaded or the FFI that might cause this breakage?</div>




<div><br></div><div>Cheers,</div><div>Mike Craig<br><br>
</div>
</blockquote></div><br></div></div></div>
</blockquote></div><br>
</div></div>