itimer: Don't free condvar until we know ticker is stopped
authorBen Gamari <ben@smart-cactus.org>
Wed, 9 Jan 2019 05:20:19 +0000 (00:20 -0500)
committerBen Gamari <ben@well-typed.com>
Mon, 14 Jan 2019 14:30:40 +0000 (09:30 -0500)
When we are shutting down the pthread ticker we signal the start_cond condition
variable to ensure that the ticker thread wakes up and exits in a reasonable
amount of time. Previously, when the ticker thread would shut down it was
responsible for freeing the start_cond condition variable. However, this would
lead to a race wherein the ticker would free start_cond, then the main thread
would try to signal it in an effort to wake the ticker (#16150).

Avoid this by moving the mutex destruction to the main thread.

rts/posix/itimer/Pthread.c

index d8f2497..817c40f 100644 (file)
@@ -147,8 +147,6 @@ static void *itimer_thread_func(void *_handle_tick)
 
     if (USE_TIMERFD_FOR_ITIMER)
         close(timerfd);
-    closeMutex(&mutex);
-    closeCondition(&start_cond);
     return NULL;
 }
 
@@ -207,6 +205,8 @@ exitTicker (bool wait)
         if (pthread_join(thread, NULL)) {
             sysErrorBelch("Itimer: Failed to join");
         }
+        closeMutex(&mutex);
+        closeCondition(&start_cond);
     } else {
         pthread_detach(thread);
     }