From e49ef4f309e631e2ecbeb7ff4397b0b073da681c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guilherme=20=C3=8Dscaro?= Date: Fri, 9 Jul 2021 17:07:18 -0300 Subject: [PATCH] darwin: fix uv_barrier race condition Prior to this patch a race condition may occur in case a thread tries to destroy the barrier while other awaiting threads were not notified. Since the internal mutex and condition variables are destroyed this may cause an undefined behavior as described by the man pages. So in order to prevent such scenarios the detroy function will not wait until all awaiting threads are finished before proceeding. Fixes: https://github.com/libuv/libuv/issues/3102 PR-URL: https://github.com/libuv/libuv/pull/3162 Reviewed-By: Jameson Nash --- src/unix/thread.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/unix/thread.c b/src/unix/thread.c index 98fc2e1b..c46450cc 100644 --- a/src/unix/thread.c +++ b/src/unix/thread.c @@ -107,8 +107,7 @@ int uv_barrier_wait(uv_barrier_t* barrier) { } last = (--b->out == 0); - if (!last) - uv_cond_signal(&b->cond); /* Not needed for last thread. */ + uv_cond_signal(&b->cond); uv_mutex_unlock(&b->mutex); return last; @@ -122,9 +121,10 @@ void uv_barrier_destroy(uv_barrier_t* barrier) { uv_mutex_lock(&b->mutex); assert(b->in == 0); - assert(b->out == 0); + while (b->out != 0) + uv_cond_wait(&b->cond, &b->mutex); - if (b->in != 0 || b->out != 0) + if (b->in != 0) abort(); uv_mutex_unlock(&b->mutex);