From 9c721bde00accd11d9fd10af59b117c0a134acda Mon Sep 17 00:00:00 2001 From: Levi Neely <141506390+lneely@users.noreply.github.com> Date: Tue, 3 Mar 2026 21:15:57 +0100 Subject: [PATCH] Fix data race on task refcount in psync_task_free (#344) Acquire mutex before reading tm->refcnt to prevent race with psync_task_dec_refcnt(). Fixes ThreadSanitizer warning. Fixes #332 Co-authored-by: Levi Neely --- pclsync/ptask.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pclsync/ptask.c b/pclsync/ptask.c index 8fe2869..4ef7a20 100644 --- a/pclsync/ptask.c +++ b/pclsync/ptask.c @@ -1181,12 +1181,13 @@ void *psync_task_papi_result(psync_task_manager_t tm, int id) { } void psync_task_free(psync_task_manager_t tm) { + int refcnt, i; + pthread_mutex_lock(&tm->mutex); if (tm->refcnt == 1) { + pthread_mutex_unlock(&tm->mutex); psync_task_destroy(tm); } else { - int refcnt, i; - pthread_mutex_lock(&tm->mutex); tm->waitfor = PSYNC_WAIT_FREED; for (i = 0; i < tm->taskcnt; i++) if (tm->tasks[i].status == PSYNC_TASK_STATUS_READY) {