From 6ac2e916eee69ef738ef1e16018a1ad5ea9aba30 Mon Sep 17 00:00:00 2001 From: Christian Spielberger Date: Tue, 31 Oct 2023 13:26:47 +0100 Subject: [PATCH] async: clear callback function pointer after use (#992) (#993) This solve some strange race condition which results in calling `cb` twice for one async work object. --- src/async/async.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/async/async.c b/src/async/async.c index 934ad0a02..9d1787647 100644 --- a/src/async/async.c +++ b/src/async/async.c @@ -68,8 +68,10 @@ static int worker_thread(void *arg) work = le->data; mtx_lock(work->mtx); - if (work->workh) + if (work->workh) { work->err = work->workh(work->arg); + work->workh = NULL; + } mtx_unlock(work->mtx); mtx_lock(&a->mtx); @@ -101,14 +103,18 @@ static void async_destructor(void *data) LIST_FOREACH(&async->workl, le) { struct async_work *work = le->data; - if (work->cb) + if (work->cb) { work->cb(ECANCELED, work->arg); + work->cb = NULL; + } } LIST_FOREACH(&async->curl, le) { struct async_work *work = le->data; - if (work->cb) + if (work->cb) { work->cb(ECANCELED, work->arg); + work->cb = NULL; + } } list_flush(&async->workl); @@ -146,8 +152,10 @@ static void queueh(int id, void *data, void *arg) (void)id; mtx_lock(work->mtx); - if (work->cb) + if (work->cb) { work->cb(work->err, work->arg); + work->cb =NULL; + } mtx_unlock(work->mtx); mtx_lock(&async->mtx);