Skip to content

Commit a8840fb

Browse files
committed
darwin: work around condition variable kernel bug
It has been reported that destroying condition variables that have been signalled but not waited on can sometimes result in application crashes. See https://codereview.chromium.org/1323293005. PR-URL: libuv#860 Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
1 parent e397caa commit a8840fb

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/unix/thread.c

+29
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,35 @@ int uv_cond_init(uv_cond_t* cond) {
392392
#endif /* defined(__APPLE__) && defined(__MACH__) */
393393

394394
void uv_cond_destroy(uv_cond_t* cond) {
395+
#if defined(__APPLE__) && defined(__MACH__)
396+
/* It has been reported that destroying condition variables that have been
397+
* signalled but not waited on can sometimes result in application crashes.
398+
* See https://codereview.chromium.org/1323293005.
399+
*/
400+
pthread_mutex_t mutex;
401+
struct timespec ts;
402+
int err;
403+
404+
if (pthread_mutex_init(&mutex, NULL))
405+
abort();
406+
407+
if (pthread_mutex_lock(&mutex))
408+
abort();
409+
410+
ts.tv_sec = 0;
411+
ts.tv_nsec = 1;
412+
413+
err = pthread_cond_timedwait_relative_np(cond, &mutex, &ts);
414+
if (err != 0 && err != ETIMEDOUT)
415+
abort();
416+
417+
if (pthread_mutex_unlock(&mutex))
418+
abort();
419+
420+
if (pthread_mutex_destroy(&mutex))
421+
abort();
422+
#endif /* defined(__APPLE__) && defined(__MACH__) */
423+
395424
if (pthread_cond_destroy(cond))
396425
abort();
397426
}

0 commit comments

Comments
 (0)