Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Given: a process binds to a port on Linux, and that process is then terminated with either TERM or KILL signals. Is there anyway that the port could still be bound, or will the kernel clean up and guarantee the port will be unbound.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

If the original process spawns child processes, it's possible for the child processes to keep the socket open past the parent process exit. If the parent process is the only process with the socket open, it should clean up upon termination. An existing process will close all open file descriptors, which includes all its sockets.

Once a close is initiated, the socket may still be in TIME_WAIT state. TCP attempts to guarantee delivery of all data being transmitted. When a socket is closed, it goes into TIME_WAIT to fulfil this guarantee. There are multiple variables that may require some time if they're TCP sockets such as to finish handshaking or whether the sockets were configured with SO_LINGER option.

Here's a link that explains TIME_WAIT from the Unix Socket FAQ: http://www.developerweb.net/forum/showthread.php?t=2941

share|improve this answer
1  
Welcome to the not-so-pretty side of TCP/IP. Many years back, I helped Addison Wesley as an editor for Stevens' books - Advanced Programming in the UNIX Environment and UNIX Network Programming - and helped improve the example programs with code to handle the edge cases such as the ones you're hitting. We did our best to document the situation, but that doesn't make the problem go away. Here's another good explanation: blog.netherlabs.nl/articles/2009/01/18/… –  pbr Dec 1 '09 at 21:11
    
Aborting a process will leave the sockets closing, going into TIME_WAIT and TIME_WAIT2 states. If the app is not written to make the sockets reusable, the port will still be bound when you try to start an app on that port. –  Tim Williscroft Dec 7 '09 at 1:24
    
Thanks for the help! –  Adam Flott Dec 8 '09 at 18:34
add comment

I know that sometimes with apache if I run apachectl stop, the process won't actually terminate fully, and I have to run a killall -9 httpd to get a clean port to bind to.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.