From 42d2e0f5c2528a636f3dfd2888d03e5b80d9a065 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sat, 27 Feb 2016 02:50:25 +1100 Subject: [PATCH] Increment client reference before adding it to the epoll list --- src/connector.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/connector.c b/src/connector.c index 627977b8..4c25f064 100644 --- a/src/connector.c +++ b/src/connector.c @@ -305,13 +305,6 @@ static int accept_client(cdata_t *cdata, const int epfd, const uint64_t server) cdata->nfds++; ck_wunlock(&cdata->lock); - event.data.u64 = client->id; - event.events = EPOLLIN | EPOLLRDHUP | EPOLLONESHOT; - if (unlikely(epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event) < 0)) { - LOGERR("Failed to epoll_ctl add in accept_client"); - return 0; - } - /* We increase the ref count on this client as epoll creates a pointer * to it. We drop that reference when the socket is closed which * removes it automatically from the epoll list. */ @@ -321,6 +314,14 @@ static int accept_client(cdata_t *cdata, const int epfd, const uint64_t server) getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &client->sendbufsize, &optlen); LOGDEBUG("Client sendbufsize detected as %d", client->sendbufsize); + event.data.u64 = client->id; + event.events = EPOLLIN | EPOLLRDHUP | EPOLLONESHOT; + if (unlikely(epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event) < 0)) { + LOGERR("Failed to epoll_ctl add in accept_client"); + dec_instance_ref(cdata, client); + return 0; + } + return 1; }