Skip to content

Commit

Permalink
aio: make sure aio is initialized before certain operations
Browse files Browse the repository at this point in the history
Operations that might be performed during teardown, such as reaping,
waiting, closing, freeing, should only be done if the aio has properly
been initialized.  This is important for certain simple cases where
inline aio objects are used, and initialization of an outer object can
fail before the enclosed aio is initialized.
  • Loading branch information
gdamore committed Nov 30, 2024
1 parent 90f88cd commit e4c8ae2
Showing 1 changed file with 33 additions and 29 deletions.
62 changes: 33 additions & 29 deletions src/core/aio.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,33 +92,35 @@ nni_aio_init(nni_aio *aio, nni_cb cb, void *arg)
void
nni_aio_fini(nni_aio *aio)
{
nni_aio_cancel_fn fn;
void *arg;
nni_aio_expire_q *eq = aio->a_expire_q;
if (aio != NULL && aio->a_expire_q != NULL) {
nni_aio_cancel_fn fn;
void *arg;
nni_aio_expire_q *eq = aio->a_expire_q;

// This is like aio_close, but we don't want to dispatch
// the task. And unlike aio_stop, we don't want to wait
// for the task. (Because we implicitly do task_fini.)
// We also wait if the aio is being expired.
nni_mtx_lock(&eq->eq_mtx);
aio->a_stop = true;
while (aio->a_expiring) {
nni_cv_wait(&eq->eq_cv);
}
nni_aio_expire_rm(aio);
fn = aio->a_cancel_fn;
arg = aio->a_cancel_arg;
aio->a_cancel_fn = NULL;
aio->a_cancel_arg = NULL;
nni_mtx_unlock(&eq->eq_mtx);
// This is like aio_close, but we don't want to dispatch
// the task. And unlike aio_stop, we don't want to wait
// for the task. (Because we implicitly do task_fini.)
// We also wait if the aio is being expired.
nni_mtx_lock(&eq->eq_mtx);
aio->a_stop = true;
while (aio->a_expiring) {
nni_cv_wait(&eq->eq_cv);

Check warning on line 107 in src/core/aio.c

View check run for this annotation

Codecov / codecov/patch

src/core/aio.c#L107

Added line #L107 was not covered by tests
}
nni_aio_expire_rm(aio);
fn = aio->a_cancel_fn;
arg = aio->a_cancel_arg;
aio->a_cancel_fn = NULL;
aio->a_cancel_arg = NULL;
nni_mtx_unlock(&eq->eq_mtx);

if (fn != NULL) {
fn(aio, arg, NNG_ECLOSED);
} else {
nni_task_abort(&aio->a_task);
}
if (fn != NULL) {
fn(aio, arg, NNG_ECLOSED);
} else {
nni_task_abort(&aio->a_task);
}

nni_task_fini(&aio->a_task);
nni_task_fini(&aio->a_task);
}
}

int
Expand All @@ -137,7 +139,7 @@ nni_aio_alloc(nni_aio **aio_p, nni_cb cb, void *arg)
void
nni_aio_free(nni_aio *aio)
{
if (aio != NULL) {
if (aio != NULL && aio->a_expire_q != NULL) {
nni_aio_fini(aio);
NNI_FREE_STRUCT(aio);
}
Expand All @@ -146,7 +148,7 @@ nni_aio_free(nni_aio *aio)
void
nni_aio_reap(nni_aio *aio)
{
if (aio != NULL) {
if (aio != NULL && aio->a_expire_q != NULL) {
nni_reap(&aio_reap_list, aio);
}
}
Expand Down Expand Up @@ -179,7 +181,7 @@ nni_aio_set_iov(nni_aio *aio, unsigned nio, const nni_iov *iov)
void
nni_aio_stop(nni_aio *aio)
{
if (aio != NULL) {
if (aio != NULL && aio->a_expire_q != NULL) {
nni_aio_cancel_fn fn;
void *arg;
nni_aio_expire_q *eq = aio->a_expire_q;
Expand All @@ -206,7 +208,7 @@ nni_aio_stop(nni_aio *aio)
void
nni_aio_close(nni_aio *aio)
{
if (aio != NULL) {
if (aio != NULL && aio->a_expire_q != NULL) {
nni_aio_cancel_fn fn;
void *arg;
nni_aio_expire_q *eq = aio->a_expire_q;
Expand Down Expand Up @@ -309,7 +311,9 @@ nni_aio_count(nni_aio *aio)
void
nni_aio_wait(nni_aio *aio)
{
nni_task_wait(&aio->a_task);
if (aio != NULL && aio->a_expire_q != NULL) {
nni_task_wait(&aio->a_task);
}
}

bool
Expand Down

0 comments on commit e4c8ae2

Please sign in to comment.