Skip to content

Commit

Permalink
Fix a race condition on shutdown
Browse files Browse the repository at this point in the history
It was possible that, during shutdown, a thread may exit prior to us
checking for it being exited, leading us to block on a read of a pipe
that was never going to be written to, causing a shutdown hang.  Fix it
by using pthread_kill to kill each thread instead

Signed-off-by: Neil Horman <nhorman@tuxdriver.com>
  • Loading branch information
nhorman committed May 23, 2019
1 parent bebc121 commit 62fbff0
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions rngd_jitter.c
Original file line number Diff line number Diff line change
Expand Up @@ -469,26 +469,22 @@ void close_jitter_entropy_source(struct rng *ent_src)
for (i=0; i < num_threads; i++)
tdata[i].active = 0;

flags = fcntl(pipefds[1], F_GETFL, 0);
flags |= O_NONBLOCK;
fcntl(pipefds[1], F_SETFL, &flags);
close(pipefds[1]);

/* And wait for completion of each thread */
for (i=0; i < num_threads; i++) {
message(LOG_DAEMON|LOG_DEBUG, "Checking on done for thread %d\n", i);
while (!tdata[i].done)
pthread_kill(threads[i], SIGINT);
if(tdata[i].done) {
message(LOG_DAEMON|LOG_INFO, "Closing thread %d\n", tdata[i].core_id);
pthread_join(threads[i], NULL);
jent_entropy_collector_free(tdata[i].ec);
} else {
read(pipefds[0], tmpbuf, 1024);
} else
sched_yield();
}
}

close(pipefds[2]);
close(pipefds[1]);
close(pipefds[0]);
free(tdata);
free(threads);
return;
Expand Down

0 comments on commit 62fbff0

Please sign in to comment.