Strange issues with jruby-ffi and zeromq

I’m using zeromq and storing per thread copies of zeromq sockets in a
class variable. Same technique used by us and others without any
issues.

Here is the code:

def self.client
unless self.clients[Thread.current.object_id]
s = Offbeat::Zmq::IPCTX.socket(ZMQ::PUSH)
s.connect(“inproc://rsyslog”)
self.clients[Thread.current.object_id] = s
end
self.clients[Thread.current.object_id]
end

When I call client, and try to send a message on the socket, it just
hangs the app. The guy who created the em-zeromq gem reported the
same problem with zeromq contexts, he had to define the context
outside of the EM.run block when using eventmachine or it would do the
same thing. He thought it was being garbage collected. Fyi I’m
pretty sure he saw this same behavior on Cruby also.

Now, when I just create the socket within the thread, and don’t stick
it in a class variable, it works fine. However I’m using rails so I
really can’t do that, I need somewhere to hold per thread socket
instances.

I’m rather stumped on this. Anyone have any ideas?

Chris

I took a thread dump as it hung, it looks like it’s hanging inside ffi
this is the last line for every socket call that’s blocking.

at com.kenai.jffi.Foreign.invokeArrayReturnInt(Native Method)

Chris

Problem solved. The context being used was a constant that was
getting defined twice because the file was required twice. This was
causing my push and pull sockets that use inproc to each get a
different context.

Chris