ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm trying to debug a program using lsof. I've been seeing a file-descriptor leak in /proc/$PID/fd from sockets that eventually keeps me from being able to create new network connections when I hit the resource limit.
lsof shows me that the socket handles that are being leaked are identified with "TYPE=sock 0,0 ...can't identify protocol" I've performed an strace and there are never any calls made (network or otherwise) that return the file-descriptors that match the bad ones in lsof.
My question is, where are these "sock" type sockets being created? Does anyone else know where they come from and how to get rid of them?
the obvious answer is that you're not closing your sockets when you're done with them. if u need them all to be open, check out setrlimit() and i think u can change max # open descriptors.
I've been down that road. I've managed to account for the opening and closing of all of the socket calls my program makes. Strace helps there. These socket handles which are being allocated do not seem be be coming from my own code, but rather internally within the system. With Strace I can see every socket I allocate and my program never allocates these. Since my program is not allocating them, I don't know what they are to close them down.
And I don't need more than a few dozen sockets open for any reason. These leaked "blank" sockets appear everytime I make a new network connection. Over time, I run out of resources so raising the limit would only delay the inevitable.
well, that's certainly odd. from stracing u have no idea what part of the code they are coming from? are you using lots of libraries? if you are doing some sort of nameresolution call before connecting(ie. gethostbyname, etc..), then of course there will be a few scokets opened for dns resolution, but i certainly hope that is not the problem or others would have encourntered already. w/o seeing code i dont know what else to tell you. how long does it take for u to max out? what kinda program is this?
This is a backup program which transmits changes to a backup server. The problem occurs with each connection I create. In the example I posted, handles 15, 16, & 18 display the problem. The speed with which I run out of resource depends on how many times I reconnect...in a test environment that can be many times an hour.
well, im baffled. all the output tells me, as u prolly know, is that the problem lies in the area of code right after creating the TCP *:1578 (LISTEN) socket, since descriptors are always assigned from next lowest open #.
Yeah, and the code of this program is complex enough its like a needle in a haystack. Even strace would only point me at a specific system call, but I was hoping that I could identify the code based on the parameters being passed, etc...
I suspect, as you mentioned earlier, that there are some internal calls which use sockets that may not be getting cleaned up. Thanks for your reponse. Perhaps someone else will come along who has my answer. I'm going to keep trying to recreate this with simpler code or try to hunt it down in some library code, or something...
Okay, I don't know if anyone's still reading this, but I had exactly the same problem rozeboom describes here and I was able to fix it.
My problem was an incorrect call to socket(). This code was written by someone else but I had to debug it. The problem was that this code failed to recognize that the function accept() opens and creates the socket without needing to call socket()
So this code was calling accept(), AND calling socket() when it should have not called socket() if it were an fd optained from accept(). FDs optained from accept() are already created and inherit their socket-parameters from the socket where accept() was called from.
In my particular case, the code's custom socket class was calling socket() from its constructor. It was doing this on sockets that'd had already been created via accept(), thus two FDs created, one of them never getting cleaned up. So my fix was to make a second version of the constructor for my socket class, one that takes an FD as a parameter. That constructor does NOT call socket(). I use this constructor to create socket class that originate from the accept() call.
It seems to work. Hope this is of use to someone out there
That sounds like it might help... the code I'm working with is also a C++ class for handling sockets and it might very well do exactly what you describe. Thanks for your response!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.