0
0
mirror of https://github.com/zeromq/libzmq.git synced 2024-12-26 23:01:04 +08:00

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.
This commit is contained in:
Patrik Wenger 2016-04-12 20:11:50 +02:00
parent b5dc794202
commit 621c965fae
2 changed files with 18 additions and 5 deletions

View File

@ -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
}

View File

@ -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);