1

This was originally posted to serverfault but was voted as off topic by the community.

Let me just preface this question by mentioning I understand the why/how/where/when of the Linux kernel socket states, and I wish to force the Linux kernel to behave a certain way for my specific application and context.

Machine is a Pi 3 B+ running Raspbian.

My goal here is to follow the answer from this question to kill sockets that are in a "useless state"*, so I implemented it in Python like so:

def kill_hanging_sockets(self):
    default_max_tcp_sockets = subprocess.check_output(['cat /proc/sys/net/ipv4/tcp_max_orphans'], shell=True)
    subprocess.call(['sudo bash -c "echo 0 > /proc/sys/net/ipv4/tcp_max_orphans"'], shell=True)
    new_max_tcp_sockets = subprocess.check_output(['cat /proc/sys/net/ipv4/tcp_max_orphans'], shell=True)
    print('new value is {}'.format(new_max_tcp_sockets))
    time.sleep(3)
    subprocess.call(['sudo bash -c "echo %s > /proc/sys/net/ipv4/tcp_max_orphans"' % default_max_tcp_sockets], shell=True)

The function is pretty simple, it just sets the value to 0 and prints the value to make sure it's 0, waits three seconds to let the kernel "kill" the orphaned sockets, then sets the max parameter back to its' original value.

However, it's not working, the sockets are not "killed" by the kernel like I presumed they would be. It seems the behavior remains the same and the socket is "killed" within 20 seconds or so; I think changing max_tcp_orpahns either doesn't affect the system, or the socket(s) in question don't truly become orphan socket.

I check the socket(s) using sudo netstat --inet -p -c and they're still there in their FIN_WAIT or CLOSE_WAIT states, which I'm trying to stop from happening. Is there something else I could try here or am I barking up the wrong tree?

[*] my definition of "useless state" may differ from yours, or the Linux kernel's, but please take into consideration the context of the question. In said context the sockets are "useless" to me because I cannot bind another socket until they disappear.

  • Well, yes. The question isn't about server administration in a professional environment, so ServerFault will have bounced it. TBH I'm not sure it's on-topic here either. – roaima Dec 20 '18 at 22:43
  • Are you creating a server and are finding issues bind()ing to the address on a restart? If so, use SO_REUSEADDR so that the old sockets in fin/close state don't prevent a new server process from listening. – Stephen Harris Dec 20 '18 at 23:03
  • @roaima where is it on-topic? I felt like Unix & Linux would be an ideal place for discussion around Linux sockets. – star_trac Dec 20 '18 at 23:29
  • @StephenHarris thanks, yes I'm using SO_REUSEADDR when creating the server socket. – star_trac Dec 20 '18 at 23:32
  • (1) Please state your objective clearly, up front.  Don’t make people jump to a footnote and read between the lines. (2) Are you asking a Why? question or a How do I do X? question?  Make sure that the question title and the question body are asking the same question. … … … … … … Please do not respond in comments; edit your question to make it clearer and more complete. – Scott Dec 22 '18 at 4:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.