Menu

#7 socket file descriptor leak

v1.0_(example)
open
nobody
None
5
2015-02-18
2015-02-18
Brian Eley
No

The makeSocket() function contains a file descriptor leak. I believe it happens if the socket() call succeeds but the connect() fails. In this case, the socket descriptor returned from socket() should be closed before returning.

I noticed this problem because on my system the network was unreachable for a few seconds every 10 minutes. After running my program for a 4 hours, the process hit the open file descriptor limit (1024 open descriptors) and could not make any further HTTP requests.

A possible fix is inserting the close() call like so:
ret = connect(sock, (struct sockaddr *)&sa, sizeof(sa));
if(ret == -1) { close(sock); errorSource = ERRNO; return -1; }

Discussion


Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.