[sheepdog] [PATCH 2/2] net: fix embryonic connection handling in connect_to()

Liu Yuan namei.unix at gmail.com
Mon Sep 3 18:39:01 CEST 2012


From: Liu Yuan <tailai.ly at taobao.com>

We should set_keepalive and set_snd_timeout before calling connect() to avoid
embrynoic connection,  which may remain in the half-open state for unbounded
periods of time (RFC 793).

This prolem can be reproduced by 035.

Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
---
 lib/net.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/lib/net.c b/lib/net.c
index 0970111..7a40c9f 100644
--- a/lib/net.c
+++ b/lib/net.c
@@ -229,24 +229,25 @@ int connect_to(const char *name, int port)
 			continue;
 		}
 
-		ret = connect(fd, res->ai_addr, res->ai_addrlen);
+		ret = set_keepalive(fd);
 		if (ret) {
-			eprintf("failed to connect to %s:%d: %m\n",
-				name, port);
 			close(fd);
-			continue;
+			break;
 		}
 
-		ret = set_keepalive(fd);
+		ret = set_snd_timeout(fd);
 		if (ret) {
+			eprintf("failed to set SO_SNDTIMEO: %m\n");
 			close(fd);
 			break;
 		}
 
-		ret = set_snd_timeout(fd);
+		ret = connect(fd, res->ai_addr, res->ai_addrlen);
 		if (ret) {
+			eprintf("failed to connect to %s:%d: %m\n",
+				name, port);
 			close(fd);
-			break;
+			continue;
 		}
 
 		ret = set_nodelay(fd);
-- 
1.7.12.84.gefa6462




More information about the sheepdog mailing list