From 36746b4f6615607510eedb8e5d168b0fc4897ded Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Thu, 13 Jul 2017 22:29:32 -0700 Subject: Close a race during pipe creation. --- src/core/endpt.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) (limited to 'src/core/endpt.c') diff --git a/src/core/endpt.c b/src/core/endpt.c index 7784de73..8eb7dd12 100644 --- a/src/core/endpt.c +++ b/src/core/endpt.c @@ -248,10 +248,19 @@ nni_ep_connect_sync(nni_ep *ep) nni_pipe *pipe; int rv; + nni_mtx_lock(&ep->ep_mtx); + if (ep->ep_closed) { + nni_mtx_unlock(&ep->ep_mtx); + return (NNG_ECLOSED); + } rv = nni_pipe_create(&pipe, ep, ep->ep_sock, ep->ep_tran); if (rv != 0) { + nni_mtx_unlock(&ep->ep_mtx); return (rv); } + nni_list_append(&ep->ep_pipes, pipe); + nni_mtx_unlock(&ep->ep_mtx); + rv = nni_ep_connect_aio(ep, &pipe->p_tran_data); if (rv != 0) { nni_pipe_stop(pipe); @@ -264,21 +273,6 @@ nni_ep_connect_sync(nni_ep *ep) return (0); } -int -nni_ep_pipe_add(nni_ep *ep, nni_pipe *pipe) -{ - nni_mtx_lock(&ep->ep_mtx); - if (ep->ep_closed) { - nni_mtx_unlock(&ep->ep_mtx); - return (NNG_ECLOSED); - } - - nni_list_append(&ep->ep_pipes, pipe); - nni_mtx_unlock(&ep->ep_mtx); - - return (0); -} - void nni_ep_pipe_remove(nni_ep *ep, nni_pipe *pipe) { @@ -426,18 +420,25 @@ nni_ep_accept_sync(nni_ep *ep) nni_pipe *pipe; int rv; + nni_mtx_lock(&ep->ep_mtx); if (ep->ep_closed) { + nni_mtx_unlock(&ep->ep_mtx); return (NNG_ECLOSED); } rv = nni_pipe_create(&pipe, ep, ep->ep_sock, ep->ep_tran); if (rv != 0) { + nni_mtx_unlock(&ep->ep_mtx); return (rv); } + nni_list_append(&ep->ep_pipes, pipe); + nni_mtx_unlock(&ep->ep_mtx); + rv = nni_ep_accept_aio(ep, &pipe->p_tran_data); if (rv != 0) { nni_pipe_stop(pipe); return (rv); } + nni_pipe_start(pipe); return (0); } -- cgit v1.2.3-70-g09d2