Commit 621c965f authored by Patrik Wenger's avatar Patrik Wenger

Problem: tricky return value from zmq::socket_poller_t::wait when poller is empty

Solution: return -1 (no event) instead of 0 (event)

For some reason, this just returns 0 if there are no sockets registered
on the poller. Usually this would mean there has been an event. So the
caller would have to check the return value AND the event, or write code
that takes the number of registered sockets into consideration.

By returning -1 and setting errno = ETIMEDOUT like in the usual timeout
cases, it's more consistent and convenient.

Test case included.
parent b5dc7942
......@@ -393,16 +393,22 @@ int zmq::socket_poller_t::wait (zmq::socket_poller_t::event_t *event_, long time
#if defined ZMQ_POLL_BASED_ON_POLL
if (unlikely (poll_size == 0)) {
// We'll report an error (timed out) as if the list was non-empty and
// no event occured within the specified timeout. Otherwise the caller
// needs to check the return value AND the event to avoid using the
// nullified event data.
errno = ETIMEDOUT;
if (timeout_ == 0)
return 0;
return -1;
#if defined ZMQ_HAVE_WINDOWS
Sleep (timeout_ > 0 ? timeout_ : INFINITE);
return 0;
return -1;
#elif defined ZMQ_HAVE_ANDROID
usleep (timeout_ * 1000);
return 0;
return -1;
#else
return usleep (timeout_ * 1000);
usleep (timeout_ * 1000);
return -1;
#endif
}
......
......@@ -60,6 +60,14 @@ int main (void)
// Set up poller
void* poller = zmq_poller_new ();
zmq_poller_event_t event;
// waiting on poller with no registered sockets should report error
rc = zmq_poller_wait(poller, &event, 0);
assert (rc == -1);
assert (errno == ETIMEDOUT);
// register sink
rc = zmq_poller_add (poller, sink, sink, ZMQ_POLLIN);
assert (rc == 0);
......@@ -69,7 +77,6 @@ int main (void)
assert (rc == 1);
// We expect a message only on the sink
zmq_poller_event_t event;
rc = zmq_poller_wait (poller, &event, -1);
assert (rc == 0);
assert (event.socket == sink);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment