aboutsummaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2017-07-18 11:00:08 -0700
committerGarrett D'Amore <garrett@damore.org>2017-07-18 11:00:08 -0700
commitab7772be3e3c208a48408b67924d3b58fca7f474 (patch)
treeb7355183556350de73303c6e75a5d278528dda0e /src/core
parentc41129ca0efacf13fe1363ed12f9723274c521e7 (diff)
downloadnng-ab7772be3e3c208a48408b67924d3b58fca7f474.tar.gz
nng-ab7772be3e3c208a48408b67924d3b58fca7f474.tar.bz2
nng-ab7772be3e3c208a48408b67924d3b58fca7f474.zip
Fix close-related leak of pipes.
We have seen leaks of pipes causing test failures (e.g. the Windows IPC test) due to EADDRINUSE. This was caused by a case where we failed to pass the pipe up because the AIO had already been canceled, and we didn't realize that we had oprhaned the pipe. The fix is to add a return value to nni_aio_finish, and verify that we did finish properly, or if we did not then we must free the pipe ourself. (The zero return from nni_aio_finish indicates that it accepts ownership of resources passed via the aio.)
Diffstat (limited to 'src/core')
-rw-r--r--src/core/aio.c7
-rw-r--r--src/core/aio.h8
-rw-r--r--src/core/endpt.c2
-rw-r--r--src/core/socket.c3
4 files changed, 12 insertions, 8 deletions
diff --git a/src/core/aio.c b/src/core/aio.c
index 73c1dd9b..c6512eb4 100644
--- a/src/core/aio.c
+++ b/src/core/aio.c
@@ -206,14 +206,14 @@ nni_aio_cancel(nni_aio *aio, int rv)
// I/O provider related functions.
-void
+int
nni_aio_finish(nni_aio *aio, int result, size_t count)
{
nni_mtx_lock(&aio->a_lk);
if (aio->a_flags & (NNI_AIO_DONE | NNI_AIO_FINI)) {
// Operation already done (canceled or timed out?)
nni_mtx_unlock(&aio->a_lk);
- return;
+ return (NNG_ESTATE);
}
aio->a_flags |= NNI_AIO_DONE;
aio->a_result = result;
@@ -228,6 +228,7 @@ nni_aio_finish(nni_aio *aio, int result, size_t count)
nni_taskq_dispatch(NULL, &aio->a_tqe);
nni_mtx_unlock(&aio->a_lk);
+ return (0);
}
void
@@ -307,7 +308,7 @@ nni_aio_expire_loop(void *arg)
nni_list *aios = &nni_aio_expire_aios;
nni_aio * aio;
nni_time now;
- int rv;
+
void (*cancelfn)(nni_aio *);
NNI_ARG_UNUSED(arg);
diff --git a/src/core/aio.h b/src/core/aio.h
index 09923d7f..4f190aa1 100644
--- a/src/core/aio.h
+++ b/src/core/aio.h
@@ -104,8 +104,12 @@ extern int nni_aio_list_active(nni_aio *);
// nni_aio_finish is called by the provider when an operation is complete.
// The provider gives the result code (0 for success, an NNG errno otherwise),
-// and the amount of data transferred (if any).
-extern void nni_aio_finish(nni_aio *, int, size_t);
+// and the amount of data transferred (if any). If the return code is
+// non-zero, it indicates that the operation failed (usually because the aio
+// was already canceled.) This is important for providers that need to
+// prevent resources (new pipes for example) from accidentally leaking
+// during close operations.
+extern int nni_aio_finish(nni_aio *, int, size_t);
// nni_aio_cancel is used to cancel an operation. Any pending I/O or
// timeouts are canceled if possible, and the callback will be returned
diff --git a/src/core/endpt.c b/src/core/endpt.c
index 4ed678d7..8048de2b 100644
--- a/src/core/endpt.c
+++ b/src/core/endpt.c
@@ -397,7 +397,7 @@ nni_ep_accept_start(nni_ep *ep)
if (ep->ep_closed) {
return;
}
-
+ aio->a_pipe = NULL;
aio->a_endpt = ep->ep_data;
ep->ep_ops.ep_accept(ep->ep_data, aio);
}
diff --git a/src/core/socket.c b/src/core/socket.c
index 7ec383d4..10bc1c80 100644
--- a/src/core/socket.c
+++ b/src/core/socket.c
@@ -124,8 +124,7 @@ nni_sock_pipe_ready(nni_sock *sock, nni_pipe *pipe)
void
nni_sock_pipe_remove(nni_sock *sock, nni_pipe *pipe)
{
- void * pdata;
- nni_ep *ep;
+ void *pdata;
pdata = nni_pipe_get_proto_data(pipe);