Skip to content

Commit

Permalink
Endpoint cancellation needs lock protection.
Browse files Browse the repository at this point in the history
  • Loading branch information
gdamore committed Aug 25, 2017
1 parent ea50bfb commit 405f6e5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
3 changes: 1 addition & 2 deletions src/core/aio.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ nni_aio_finish_impl(
{
nni_mtx_lock(&nni_aio_lk);

// provider only calls us *once*, but expiration may be in flight
NNI_ASSERT(aio->a_expiring || aio->a_pend == 0);
NNI_ASSERT(aio->a_pend == 0); // provider only calls us *once*

nni_list_node_remove(&aio->a_expire_node);
aio->a_pend = 1;
Expand Down
19 changes: 15 additions & 4 deletions src/core/endpt.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct nni_ep {
int ep_closed; // full shutdown
int ep_closing; // close pending (waiting on refcnt)
int ep_refcnt;
int ep_tmo_run;
nni_mtx ep_mtx;
nni_cv ep_cv;
nni_list ep_pipes;
Expand Down Expand Up @@ -243,6 +244,7 @@ nni_ep_shutdown(nni_ep *ep)
return (NNG_ECLOSED);
}
ep->ep_closing = 1;
nni_mtx_unlock(&ep->ep_mtx);

// Abort any remaining in-flight operations.
nni_aio_cancel(&ep->ep_acc_aio, NNG_ECLOSED);
Expand All @@ -253,8 +255,6 @@ nni_ep_shutdown(nni_ep *ep)
// Stop the underlying transport.
ep->ep_ops.ep_close(ep->ep_data);

nni_mtx_unlock(&ep->ep_mtx);

return (0);
}

Expand Down Expand Up @@ -294,8 +294,16 @@ nni_ep_close(nni_ep *ep)
static void
nni_ep_tmo_cancel(nni_aio *aio, int rv)
{
nni_ep *ep = aio->a_prov_data;
// The only way this ever gets "finished", is via cancellation.
nni_aio_finish_error(aio, rv);
if (ep != NULL) {
nni_mtx_lock(&ep->ep_mtx);
if (ep->ep_tmo_run) {
nni_aio_finish_error(aio, rv);
}
ep->ep_tmo_run = 0;
nni_mtx_unlock(&ep->ep_mtx);
}
}

static void
Expand All @@ -322,7 +330,10 @@ nni_ep_tmo_start(nni_ep *ep)

ep->ep_tmo_aio.a_expire =
nni_clock() + (backoff ? nni_random() % backoff : 0);
nni_aio_start(&ep->ep_tmo_aio, nni_ep_tmo_cancel, ep);
ep->ep_tmo_run = 1;
if (nni_aio_start(&ep->ep_tmo_aio, nni_ep_tmo_cancel, ep) != 0) {
ep->ep_tmo_run = 0;
}
}

static void
Expand Down

0 comments on commit 405f6e5

Please sign in to comment.