From b831d6ac5b402e2874bc79c50a793596986f944a Mon Sep 17 00:00:00 2001 From: Simon Pichugin Date: Mar 02 2018 12:00:25 +0000 Subject: Issue 49572 - ns_job_wait race on condvar Bug description: ns_job_persist_disarm_test were failing because of a race condition. Fix description: We need to lock the cb mutex before we arm the job so the order of operations that happens there is determined. First we lock the mutex, then after the rearm it will disarm it and will wait inside for the mutex unlock. In a meanwhile, our testing thread will come to cond_wait and it will unlock the mutex while waiting for the signal. The mutex inside of callback will be acquired and cb_check will set to 1 and the signal will be sent. After that, cond_wait will be released and the test will check for the right cb_check value. https://pagure.io/389-ds-base/issue/49572 Reviewed by: wibrown (Thanks!) --- diff --git a/src/nunc-stans/test/test_nuncstans.c b/src/nunc-stans/test/test_nuncstans.c index afe3c02..27fd98e 100644 --- a/src/nunc-stans/test/test_nuncstans.c +++ b/src/nunc-stans/test/test_nuncstans.c @@ -313,6 +313,7 @@ ns_job_persist_disarm_test(void **state) struct ns_job_t *job = NULL; struct timespec timeout = {2, 0}; + pthread_mutex_lock(&cb_lock); assert_int_equal( ns_create_job(tp, NS_JOB_NONE | NS_JOB_PERSIST, ns_init_disarm_job_cb, &job), NS_SUCCESS); @@ -320,7 +321,6 @@ ns_job_persist_disarm_test(void **state) assert_int_equal(ns_job_rearm(job), NS_SUCCESS); /* In the callback it should disarm */ - pthread_mutex_lock(&cb_lock); assert(cond_wait_rel(&cb_cond, &cb_lock, &timeout) == 0); pthread_mutex_unlock(&cb_lock); /* Make sure it did */