Skip to content

Commit

Permalink
Fixed issue 2278, loop->terminated == true when restarting a loop, al…
Browse files Browse the repository at this point in the history
…lowing timer callbacks to update loop->timers in place.
  • Loading branch information
Andrew-Ellison committed Mar 27, 2024
1 parent db94044 commit e245442
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/zloop.c
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@ zloop_start (zloop_t *self)
{
assert (self);
int rc = 0;
self->terminated = false;

// Main reactor loop
while (!zsys_interrupted || self->nonstop) {
Expand Down Expand Up @@ -915,6 +916,24 @@ s_timer_event5 (zloop_t *loop, int timer_id, void *arg)
return 0;
}

static int
s_timer_event6 (zloop_t *loop, int timer_id, void *arg)
{
*((bool *) arg) = true;
return -1;
}

static int
s_timer_event7 (zloop_t *loop, int timer_id, void *arg)
{
// If this happens while loop->terminated == true, the new timer will be skipped,
// as loop->timers will be updated in place.
zloop_timer_end (loop, timer_id);
zloop_timer (loop, 0, 1, s_timer_event6, arg);

return 0;
}

void
zloop_test (bool verbose)
{
Expand Down Expand Up @@ -989,6 +1008,25 @@ zloop_test (bool verbose)
zloop_start (loop);
assert (!socket_event_called);

// Check if timer destroyed in reactor callback, the lazy list is used
zloop_destroy (&loop);
loop = zloop_new ();
zloop_set_verbose (loop, verbose);

zloop_timer (loop, 0, 1, s_timer_event4, NULL);
zloop_start (loop);
assert (loop->terminated);

bool timer_end_called_1 = false, timer_end_called_2 = false;

// This timer should never be called, as the timer created in s_timer_event_7 should finish first
zloop_timer (loop, 10, 1, s_timer_event6, &timer_end_called_1);
zloop_timer (loop, 2, 5, s_timer_event7, &timer_end_called_2);
zloop_start (loop);

assert(timer_end_called_1 == false);
assert(timer_end_called_2 == true);

// cleanup
zloop_destroy (&loop);
assert (loop == NULL);
Expand Down

0 comments on commit e245442

Please sign in to comment.