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 <lkn@darkstar.example.net>
This commit is contained in:
Levi Neely 2026-03-03 21:15:57 +01:00 committed by GitHub
parent ede53b6b89
commit 9c721bde00
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 3 additions and 2 deletions

View File

@ -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) { void psync_task_free(psync_task_manager_t tm) {
int refcnt, i;
pthread_mutex_lock(&tm->mutex);
if (tm->refcnt == 1) { if (tm->refcnt == 1) {
pthread_mutex_unlock(&tm->mutex);
psync_task_destroy(tm); psync_task_destroy(tm);
} }
else { else {
int refcnt, i;
pthread_mutex_lock(&tm->mutex);
tm->waitfor = PSYNC_WAIT_FREED; tm->waitfor = PSYNC_WAIT_FREED;
for (i = 0; i < tm->taskcnt; i++) for (i = 0; i < tm->taskcnt; i++)
if (tm->tasks[i].status == PSYNC_TASK_STATUS_READY) { if (tm->tasks[i].status == PSYNC_TASK_STATUS_READY) {