Skip to content

Commit

Permalink
Lock thread_pool.wait_mutex before forking to avoid a race condition …
Browse files Browse the repository at this point in the history
…between rad_fork, rad_waitpid and reap_children.

There is a race condition that can occur under high load where a child is reaped before being added to the waiters list.
  • Loading branch information
jrouzierinverse authored and alandekok committed Oct 28, 2014
1 parent 5fddf69 commit 98d0117
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/main/threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,7 @@ pid_t rad_fork(void)
/*
* Fork & save the PID for later reaping.
*/
pthread_mutex_lock(&thread_pool.wait_mutex);
child_pid = fork();
if (child_pid > 0) {
int rcode;
Expand All @@ -1268,16 +1269,20 @@ pid_t rad_fork(void)

tf->pid = child_pid;

pthread_mutex_lock(&thread_pool.wait_mutex);
rcode = fr_hash_table_insert(thread_pool.waiters, tf);
pthread_mutex_unlock(&thread_pool.wait_mutex);

if (!rcode) {
ERROR("Failed to store PID, creating what will be a zombie process %d",
(int) child_pid);
free(tf);
}
}
/*
* Do not unlock in child process
*/
if(child_pid != 0 ) {
pthread_mutex_unlock(&thread_pool.wait_mutex);
}

/*
* Return whatever we were told.
Expand Down

0 comments on commit 98d0117

Please sign in to comment.