[sheepdog] [PATCH] sheep/request: fix fd leak

Ruoyu liangry at ucweb.com
Thu Jul 31 05:29:02 CEST 2014


This patch fixes fd leak in sheep process such as below in lsof command.

sheep 15276 liangry 95u sock 0,7 0t0 465109 can't identify protocol
sheep 15276 liangry 96u sock 0,7 0t0 464282 can't identify protocol
sheep 15276 liangry 97u sock 0,7 0t0 464284 can't identify protocol

Signed-off-by: Ruoyu <liangry at ucweb.com>
---
 sheep/request.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/sheep/request.c b/sheep/request.c
index 2a24b93..a4d8ed0 100644
--- a/sheep/request.c
+++ b/sheep/request.c
@@ -729,8 +729,14 @@ main_fn void put_request(struct request *req)
 		eventfd_xwrite(req->local_req_efd, 1);
 	else {
 		if (ci->conn.dead) {
-			clear_client_info(ci);
+			/*
+			 * free_request should be called prior to
+			 * clear_client_info because refcnt of ci will
+			 * be decreased in free_request. Otherwise, ci
+			 * cannot be freed in clear_client_info.
+			 */
 			free_request(req);
+			clear_client_info(ci);
 		} else {
 			list_add_tail(&req->request_list, &ci->done_reqs);
 
@@ -966,7 +972,14 @@ static void client_handler(int fd, int events, void *data)
 
 	sd_debug("%x, %d", events, ci->conn.dead);
 
-	if (events & (EPOLLERR | EPOLLHUP) || ci->conn.dead)
+	if (events & (EPOLLERR | EPOLLHUP))
+		ci->conn.dead = true;
+	/*
+	 * Although dead is true, ci might not be freed immediately
+	 * because of refcnt. Never mind, we will complete it later
+	 * as long as dead is true.
+	 */
+	if (ci->conn.dead)
 		return clear_client_info(ci);
 
 	if (events & EPOLLIN) {
-- 
1.8.3.2





More information about the sheepdog mailing list