diff options
Diffstat (limited to 'src/protocol')
| -rw-r--r-- | src/protocol/pair/pair.c | 28 | ||||
| -rw-r--r-- | src/protocol/reqrep/req.c | 20 |
2 files changed, 34 insertions, 14 deletions
diff --git a/src/protocol/pair/pair.c b/src/protocol/pair/pair.c index 98995186..306615b7 100644 --- a/src/protocol/pair/pair.c +++ b/src/protocol/pair/pair.c @@ -87,7 +87,10 @@ nni_pair_add_pipe(void *arg, nni_pipe *pipe, void **datap) nni_pair_pipe *pp; int rv; - pp = nni_alloc(sizeof (*pp)); + if ((pp = nni_alloc(sizeof (*pp))) == NULL) { + return (NNG_ENOMEM); + } + pp->pipe = pipe; pp->good = 0; pp->sigclose = 0; @@ -97,24 +100,31 @@ nni_pair_add_pipe(void *arg, nni_pipe *pipe, void **datap) nni_mutex_enter(&pair->mx); if (pair->pipe != NULL) { - // Already have a peer, denied. - nni_mutex_exit(&pair->mx); - nni_free(pp, sizeof (*pp)); - return (NNG_EBUSY); + rv = NNG_EBUSY; // Already have a peer, denied. + goto fail; } if ((rv = nni_thread_create(&pp->rthr, nni_pair_receiver, pp)) != 0) { - nni_mutex_exit(&pair->mx); - return (rv); + goto fail; } if ((rv = nni_thread_create(&pp->sthr, nni_pair_sender, pp)) != 0) { - nni_mutex_exit(&pair->mx); - return (rv); + goto fail; } pp->good = 1; pair->pipe = pp; *datap = pp; nni_mutex_exit(&pair->mx); return (0); + +fail: + nni_mutex_exit(&pair->mx); + if (pp->rthr != NULL) { + nni_thread_reap(pp->rthr); + } + if (pp->sthr != NULL) { + nni_thread_reap(pp->sthr); + } + nni_free(pp, sizeof (*pp)); + return (rv); } diff --git a/src/protocol/reqrep/req.c b/src/protocol/reqrep/req.c index b0225e90..54ac7b11 100644 --- a/src/protocol/reqrep/req.c +++ b/src/protocol/reqrep/req.c @@ -121,7 +121,9 @@ nni_req_add_pipe(void *arg, nni_pipe *pipe, void **datap) nni_req_pipe *rp; int rv; - rp = nni_alloc(sizeof (*rp)); + if ((rp = nni_alloc(sizeof (*rp))) == NULL) { + return (NNG_ENOMEM); + } rp->pipe = pipe; rp->good = 0; rp->sigclose = 0; @@ -131,18 +133,26 @@ nni_req_add_pipe(void *arg, nni_pipe *pipe, void **datap) nni_mutex_enter(&req->mx); if ((rv = nni_thread_create(&rp->rthr, nni_req_receiver, rp)) != 0) { - nni_mutex_exit(&req->mx); - return (rv); + goto fail; } if ((rv = nni_thread_create(&rp->sthr, nni_req_sender, rp)) != 0) { - nni_mutex_exit(&req->mx); - return (rv); + goto fail; } rp->good = 1; nni_list_append(&req->pipes, rp); *datap = rp; nni_mutex_exit(&req->mx); return (0); +fail: + nni_mutex_exit(&req->mx); + if (rp->rthr) { + nni_thread_reap(rp->rthr); + } + if (rp->sthr) { + nni_thread_reap(rp->sthr); + } + nni_free(rp, sizeof (*rp)); + return (rv); } |
