Skip to contentSkip to conversation

alfred: Close socket after connect failure

PATCH Applied Sven Eckelmann 2 participants 2 messages Feb 5, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 alfred: Close socket after connect failure

The socket is still using one file descriptor of the process when the connection fails. This fd leak is non-problematic in the current client process code because the client would be closed in this situation anyway and therefore the socket would be closed when the process is stopped. But people using this as example code may not stop the process on this error. The unclosed and unused sockets would accumulate and the process would hit the limit for open file descriptors.

---
 gpsd/alfred-gpsd.c | 2 ++
 unix_sock.c        | 2 ++
 vis/vis.c          | 2 ++
 3 files changed, 6 insertions(+)
gpsd/alfred-gpsd.c +2 -0
index d736ad1..4bc18ae 100644
@@ -40,6 +40,8 @@ static int alfred_open_sock(struct globals *globals)
 
 	if (connect(globals->unix_sock, (struct sockaddr *)&addr,
 		    sizeof(addr)) < 0) {
+		close(globals->unix_sock);
+		globals->unix_sock = -1;
 		perror("can't connect to unix socket");
 		return -1;
 	}
unix_sock.c +2 -0
index 8a99327..693b55d 100644
@@ -84,6 +84,8 @@ int unix_sock_open_client(struct globals *globals)
 
 	if (connect(globals->unix_sock, (struct sockaddr *)&addr,
 		    sizeof(addr)) < 0) {
+		close(globals->unix_sock);
+		globals->unix_sock = -1;
 		perror("can't connect to unix socket");
 		return -1;
 	}
vis/vis.c +2 -0
index 3dd5c8c..1916ea3 100644
@@ -173,6 +173,8 @@ static int alfred_open_sock(struct globals *globals)
 
 	if (connect(globals->unix_sock, (struct sockaddr *)&addr,
 		    sizeof(addr)) < 0) {
+		close(globals->unix_sock);
+		globals->unix_sock = -1;
 		perror("can't connect to unix socket");
 		return -1;
 	}
Signature · 1 line
2.1.4
#1 lore raw
Quoted 9 lines · On Thursday 05 February 2015 15:47:13 Sven Eckelmann wrote:

The socket is still using one file descriptor of the process when the connection fails. This fd leak is non-problematic in the current client process code because the client would be closed in this situation anyway and therefore the socket would be closed when the process is stopped. But people using this as example code may not stop the process on this error. The unclosed and unused sockets would accumulate and the process would hit the limit for open file descriptors.

applied in revision 26a7b5e.

Thanks!
Simon