[zeromq-dev] zloop_timer() does not fire if added after zloop_start()...

Andy Ballingall TF ballingall at thefoundry.co.uk
Thu Dec 27 11:20:50 CET 2012


CZMQ's zloop reactor (  http://czmq.zeromq.org/manual:zloop ) is a
handy thing, but one thing caught me out.

In my design, after starting a reactor (with zloop_start()), I wanted
to later add a timer (with zloop_timer() ). However, this timer didn't
appear to fire when expected (or at all).

After looking at the source code, it became clear why. (See line 357,
https://github.com/zeromq/czmq/blob/master/src/zloop.c ). The reactor
implementation is a 'tickless' design which calls zmq_poll() with a
timeout (s_tickless_timer) equalling the first expiring timer. If
there are no timers, then it'll default to an hour. So if you add a
timer once it's running, then because it's already in zmq_poll with a
timeout of an hour, it doesn't have the chance to exit the zmq_poll
and update s_tickless_timer.

In my case, I changed my design slightly to work with a heartbeat
timer which was added before calling zloop_start(), but I can see
other scenarios where it would be good to be able to add and remove
timers after calling zloop_start(), and be guaranteed that the timers
would fire when required.

This issue may be masked in those cases where a timer is added to a
reactor which already has a timer in place, because when the first
timer expires, it gives the reactor the chance to recalculate the
correct s_tickless_timer, taking into account the timer you've just
added. If the first timer should fire first, you won't notice the
issue. However, if the second timer should fire first, it will miss
that first firing.

There's no mention of this shortcoming in the docs, but It doesn't
look like an easy thing to solve, so maybe just mentioning it in the
documentation would be enough to help others who might run into the
issue... or is there another way to add and remove timers dynamically
once zloop_start() has begin and be sure that each timer first when

This slight issue aside, the zloop reactor is a lovely thing. I'm
currently using it in 2 places and it saves many lines of code.


Andy Ballingall
Senior Software Engineer

The Foundry
6th Floor, The Communications Building,
48, Leicester Square,
London, WC2H 7LT, UK
Tel: +44 (0)20 7968 6828 - Fax: +44 (0)20 7930 8906
Web: http://www.thefoundry.co.uk/

The Foundry Visionmongers Ltd.
Registered in England and Wales No: 4642027

More information about the zeromq-dev mailing list