Skip to content

Commit

Permalink
reproc: Fix deadline bugs
Browse files Browse the repository at this point in the history
Some of the deadline related logic was just plain wrong.
  • Loading branch information
DaanDeMeyer committed Aug 30, 2020
1 parent d722370 commit 9224065
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions reproc/src/reproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ static int expiry(int timeout, int64_t deadline)
int64_t n = now();

if (n >= deadline) {
return 0;
return REPROC_DEADLINE;
}

// `deadline` exceeds `now` by at most a full `int` so the cast is safe.
Expand Down Expand Up @@ -127,6 +127,10 @@ static size_t find_earliest_deadline(reproc_event_source *sources,

int current = expiry(REPROC_INFINITE, process->deadline);

if (current == REPROC_DEADLINE) {
return i;
}

if (min == REPROC_INFINITE || current < min) {
earliest = i;
min = current;
Expand Down Expand Up @@ -302,7 +306,11 @@ int reproc_poll(reproc_event_source *sources, size_t num_sources, int timeout)
? REPROC_INFINITE
: sources[earliest].process->deadline;

if (deadline == 0) {
int first = expiry(timeout, deadline);
size_t num_pipes = num_sources * PIPES_PER_SOURCE;
int r = REPROC_ENOMEM;

if (first == REPROC_DEADLINE) {
for (size_t i = 0; i < num_sources; i++) {
sources[i].events = 0;
}
Expand All @@ -311,10 +319,6 @@ int reproc_poll(reproc_event_source *sources, size_t num_sources, int timeout)
return 1;
}

int first = expiry(timeout, deadline);
size_t num_pipes = num_sources * PIPES_PER_SOURCE;
int r = REPROC_ENOMEM;

pipe_event_source *pipes = calloc(num_pipes, sizeof(pipe_event_source));
if (pipes == NULL) {
return r;
Expand Down Expand Up @@ -368,7 +372,7 @@ int reproc_poll(reproc_event_source *sources, size_t num_sources, int timeout)
sources[i].events = 0;
}

if (r == 0 && first == deadline) {
if (r == 0 && first != timeout) {
// Differentiate between timeout and deadline expiry. Deadline expiry is an
// event, timeouts are not.
sources[earliest].events = REPROC_EVENT_DEADLINE;
Expand Down Expand Up @@ -550,9 +554,12 @@ int reproc_wait(reproc_t *process, int timeout)
}

if (timeout == REPROC_DEADLINE) {
// If the deadline has expired, `expiry` returns 0 which means we'll only
// check if the process is still running.
timeout = expiry(REPROC_INFINITE, process->deadline);
// If the deadline has expired, `expiry` returns `REPROC_DEADLINE` which
// means we'll only check if the process is still running.
if (timeout == REPROC_DEADLINE) {
timeout = 0;
}
}

ASSERT(process->pipe.exit != PIPE_INVALID);
Expand Down

0 comments on commit 9224065

Please sign in to comment.