[zeromq-dev] Using ZMQ_FD with select

Yi Ding yi.s.ding at gmail.com
Sun Feb 19 00:42:37 CET 2012

On Sat, Feb 18, 2012 at 5:32 PM, Yi Ding <yi.s.ding at gmail.com> wrote:
> On Sat, Feb 18, 2012 at 5:31 PM, Yi Ding <yi.s.ding at gmail.com> wrote:
>> On Sat, Feb 18, 2012 at 8:59 AM, Schmurfy <schmurfy at gmail.com> wrote:
>>> I have a strange problem related to using the file descriptor with an event
>>> loop which led me to do some testing with it  in C but there is still
>>> something I cannot figure out.
>>> I created a simple client and server, the client (req.c) sends a request and
>>> wait for an answer while the server just sends back what was received, my
>>> problem is that the client (req.c) always timeout while doing a select on
>>> the file descriptor and I cannot figure out what is wrong in my code.
>>> Another thing that bother me is that the file descriptor is edge-triggered
>>> as specified in the documentation but what happens if I "miss" the change ?
>>> I am not sure if this is possible but let's say I was doing something else
>>> when the data was receiving, if I do a select after will it block forever ?
>>> Here is my code (I used czmq to make the code easier to write and read, I
>>> used zeromq 3.1 for this test): https://gist.github.com/1859646
>>> Thanks for any answer I hate when I don't understand something ^^
>> I've run into this exact issue Julien.  The advice I've gotten is to
>> always call recv after send prior to calling select.  I find it
>> counterintuitive, but that's the way the devs have said it should be
>> done.
>> I don't think it's possible to miss a change, as long as you've called
>> recv the exact right number of times. (don't call recv again after
>> successfully recv'ing a message, unless you're looking to get a EFSM,
>> no matter what people on the list might say)  The edge triggering will
>> remain valid as long as you don't select on it.
> Btw, this exact issue gave me so much trouble that I ended up just
> switching from REP-REQ to PUSH-PULL.

OK, major foot in mouth.  Should have looked at the code before
e-mailing the list.

Your problem is here:

int ret = select(1, &read_set, NULL, NULL, &tv);

It should instead be:

int ret = select(fd + 1, &read_set, NULL, NULL, &tv);

The creators of select wisely chose to confuse people by naming the
parameter numfds instead of oneplusmaxfd which is what it really
should be named.

More information about the zeromq-dev mailing list