Skip to content

Commit

Permalink
sd-event: change ordering of pending/ratelimited events
Browse files Browse the repository at this point in the history
Instead of ordering non-pending before pending we should order
"non-pending OR ratelimited" before "pending AND not-ratelimited".
This fixes a bug where ratelimited events were ordered at the end of the
priority queue and could be stuck there for an indeterminate amount of
time.
  • Loading branch information
poettering authored and anitazha committed Jun 8, 2021
1 parent c28c1a7 commit 4a96ebe
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/libsystemd/sd-event/sd-event.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,14 @@ static usec_t time_event_source_next(const sd_event_source *s) {
return USEC_INFINITY;
}

static bool event_source_timer_candidate(const sd_event_source *s) {
assert(s);

/* Returns true for event sources that either are not pending yet (i.e. where it's worth to mark them pending)
* or which are currently ratelimited (i.e. where it's worth leaving the ratelimited state) */
return !s->pending || s->ratelimited;
}

static int earliest_time_prioq_compare(const void *a, const void *b) {
const sd_event_source *x = a, *y = b;

Expand All @@ -246,10 +254,9 @@ static int earliest_time_prioq_compare(const void *a, const void *b) {
if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
return 1;

/* Move the pending ones to the end */
if (!x->pending && y->pending)
if (event_source_timer_candidate(x) && !event_source_timer_candidate(y))
return -1;
if (x->pending && !y->pending)
if (!event_source_timer_candidate(x) && event_source_timer_candidate(y))
return 1;

/* Order by time */
Expand Down

0 comments on commit 4a96ebe

Please sign in to comment.