diff options
| author | Garrett D'Amore <garrett@damore.org> | 2018-04-18 20:38:00 -0700 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2018-04-20 07:34:16 -0700 |
| commit | 5902d02ad0a056a146231568f1293ffbcd59f61c (patch) | |
| tree | be38584c02d703ec2322ab941d4d723c752fe187 /src/transport/ipc | |
| parent | 40542e7af0f5003d7ad67876ea580a59174031ca (diff) | |
| download | nng-5902d02ad0a056a146231568f1293ffbcd59f61c.tar.gz nng-5902d02ad0a056a146231568f1293ffbcd59f61c.tar.bz2 nng-5902d02ad0a056a146231568f1293ffbcd59f61c.zip | |
fixes #346 nng_recv() sometimes acts on null `msg` pointer
This closes a fundamental flaw in the way aio structures were
handled. In paticular, aio expiration could race ahead, and
fire before the aio was properly registered by the provider.
This ultimately led to the possibility of duplicate completions
on the same aio.
The solution involved breaking up nni_aio_start into two functions.
nni_aio_begin (which can be run outside of external locks) simply
validates that nni_aio_fini() has not been called, and clears certain
fields in the aio to make it ready for use by the provider.
nni_aio_schedule does the work to register the aio with the expiration
thread, and should only be called when the aio is actually scheduled
for asynchronous completion. nni_aio_schedule_verify does the same thing,
but returns NNG_ETIMEDOUT if the aio has a zero length timeout.
This change has a small negative performance impact. We have plans to
rectify that by converting nni_aio_begin to use a locklesss flag for
the aio->a_fini bit.
While we were here, we fixed some error paths in the POSIX subsystem,
which would have returned incorrect error codes, and we made some
optmizations in the message queues to reduce conditionals while holding
locks in the hot code path.
Diffstat (limited to 'src/transport/ipc')
| -rw-r--r-- | src/transport/ipc/ipc.c | 47 |
1 files changed, 20 insertions, 27 deletions
diff --git a/src/transport/ipc/ipc.c b/src/transport/ipc/ipc.c index ecc9a962..61b89f20 100644 --- a/src/transport/ipc/ipc.c +++ b/src/transport/ipc/ipc.c @@ -418,11 +418,11 @@ nni_ipc_pipe_send(void *arg, nni_aio *aio) { nni_ipc_pipe *pipe = arg; - nni_mtx_lock(&pipe->mtx); - if (nni_aio_start(aio, nni_ipc_cancel_tx, pipe) != 0) { - nni_mtx_unlock(&pipe->mtx); + if (nni_aio_begin(aio) != 0) { return; } + nni_mtx_lock(&pipe->mtx); + nni_aio_schedule(aio, nni_ipc_cancel_tx, pipe); nni_list_append(&pipe->sendq, aio); if (nni_list_first(&pipe->sendq) == aio) { nni_ipc_pipe_dosend(pipe, aio); @@ -474,12 +474,13 @@ nni_ipc_pipe_recv(void *arg, nni_aio *aio) { nni_ipc_pipe *pipe = arg; - nni_mtx_lock(&pipe->mtx); - - if (nni_aio_start(aio, nni_ipc_cancel_rx, pipe) != 0) { - nni_mtx_unlock(&pipe->mtx); + if (nni_aio_begin(aio) != 0) { return; } + nni_mtx_lock(&pipe->mtx); + + // Transports never have a zero length timeout. + (void) nni_aio_schedule(aio, nni_ipc_cancel_rx, pipe); nni_list_append(&pipe->recvq, aio); if (nni_list_first(&pipe->recvq) == aio) { @@ -492,10 +493,12 @@ static void nni_ipc_pipe_start(void *arg, nni_aio *aio) { nni_ipc_pipe *pipe = arg; - int rv; nni_aio * negaio; nni_iov iov; + if (nni_aio_begin(aio) != 0) { + return; + } nni_mtx_lock(&pipe->mtx); pipe->txhead[0] = 0; pipe->txhead[1] = 'S'; @@ -513,11 +516,7 @@ nni_ipc_pipe_start(void *arg, nni_aio *aio) iov.iov_len = 8; iov.iov_buf = &pipe->txhead[0]; nni_aio_set_iov(negaio, 1, &iov); - rv = nni_aio_start(aio, nni_ipc_cancel_start, pipe); - if (rv != 0) { - nni_mtx_unlock(&pipe->mtx); - return; - } + nni_aio_schedule(aio, nni_ipc_cancel_start, pipe); nni_plat_ipc_pipe_send(pipe->ipp, negaio); nni_mtx_unlock(&pipe->mtx); } @@ -680,16 +679,14 @@ static void nni_ipc_ep_accept(void *arg, nni_aio *aio) { nni_ipc_ep *ep = arg; - int rv; - - nni_mtx_lock(&ep->mtx); - NNI_ASSERT(ep->user_aio == NULL); - if ((rv = nni_aio_start(aio, nni_ipc_cancel_ep, ep)) != 0) { - nni_mtx_unlock(&ep->mtx); + if (nni_aio_begin(aio) != 0) { return; } + nni_mtx_lock(&ep->mtx); + NNI_ASSERT(ep->user_aio == NULL); + nni_aio_schedule(aio, nni_ipc_cancel_ep, ep); ep->user_aio = aio; nni_plat_ipc_ep_accept(ep->iep, ep->aio); @@ -700,18 +697,14 @@ static void nni_ipc_ep_connect(void *arg, nni_aio *aio) { nni_ipc_ep *ep = arg; - int rv; - - nni_mtx_lock(&ep->mtx); - NNI_ASSERT(ep->user_aio == NULL); - // If we can't start, then its dying and we can't report - // either. - if ((rv = nni_aio_start(aio, nni_ipc_cancel_ep, ep)) != 0) { - nni_mtx_unlock(&ep->mtx); + if (nni_aio_begin(aio) != 0) { return; } + nni_mtx_lock(&ep->mtx); + NNI_ASSERT(ep->user_aio == NULL); + nni_aio_schedule(aio, nni_ipc_cancel_ep, ep); ep->user_aio = aio; nni_plat_ipc_ep_connect(ep->iep, ep->aio); |
