From 09b31812fa2af4e67d9d9193aaae0d7111ded15f Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sun, 16 Jul 2017 12:36:32 -0700 Subject: Fix locking errors in endpoints, and simplify some logic. This cleans up the pipe creation logic greatly, and eliminates a nasty potential deadlock (lock-order incorrect.) It also adds a corret binary exponential and randomized backoff on both accept and connect. --- src/core/socket.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src/core/socket.c') diff --git a/src/core/socket.c b/src/core/socket.c index 75ae8450..ecddf5dd 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -757,6 +757,11 @@ nni_sock_dial(nni_sock *sock, const char *addr, nni_ep **epp, int flags) return (rv); } nni_list_append(&sock->s_eps, ep); + // Put a hold on the endpoint, for now. + nni_mtx_lock(&ep->ep_mtx); + ep->ep_refcnt++; + ep->ep_started = 1; + nni_mtx_unlock(&ep->ep_mtx); nni_mtx_unlock(&sock->s_mx); if ((rv = nni_ep_dial(ep, flags)) != 0) { @@ -765,6 +770,15 @@ nni_sock_dial(nni_sock *sock, const char *addr, nni_ep **epp, int flags) *epp = ep; } + // Drop our endpoint hold. + nni_mtx_lock(&ep->ep_mtx); + if (rv != 0) { + ep->ep_started = 0; + } + ep->ep_refcnt--; + nni_cv_wake(&ep->ep_cv); + nni_mtx_unlock(&ep->ep_mtx); + return (rv); } @@ -779,7 +793,12 @@ nni_sock_listen(nni_sock *sock, const char *addr, nni_ep **epp, int flags) nni_mtx_unlock(&sock->s_mx); return (rv); } + nni_list_append(&sock->s_eps, ep); + nni_mtx_lock(&ep->ep_mtx); + ep->ep_refcnt++; + ep->ep_started = 1; + nni_mtx_unlock(&ep->ep_mtx); nni_mtx_unlock(&sock->s_mx); if ((rv = nni_ep_listen(ep, flags)) != 0) { @@ -788,6 +807,15 @@ nni_sock_listen(nni_sock *sock, const char *addr, nni_ep **epp, int flags) *epp = ep; } + // Drop our endpoint hold. + nni_mtx_lock(&ep->ep_mtx); + if (rv != 0) { + ep->ep_started = 0; + } + ep->ep_refcnt--; + nni_cv_wake(&ep->ep_cv); + nni_mtx_unlock(&ep->ep_mtx); + return (rv); } -- cgit v1.2.3-70-g09d2