[sheepdog] [PATCH stable-0.8] sheep/request: fix fd leak

Hitoshi Mitake mitake.hitoshi at lab.ntt.co.jp
Wed Aug 13 08:14:32 CEST 2014


From: Ruoyu <liangry at ucweb.com>

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>
Signed-off-by: Liu Yuan <namei.unix at gmail.com>
---
 sheep/request.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/sheep/request.c b/sheep/request.c
index 8fc378a..8a71dc2 100644
--- a/sheep/request.c
+++ b/sheep/request.c
@@ -697,8 +697,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);
 
@@ -915,7 +921,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