Skip to content

Commit

Permalink
async: clear callback function pointer after use (#992) (#993)
Browse files Browse the repository at this point in the history
This solve some strange race condition which results in calling `cb` twice for
one async work object.
  • Loading branch information
cspiel1 authored Oct 31, 2023
1 parent 4bb4a34 commit 6ac2e91
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/async/async.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 6ac2e91

Please sign in to comment.