Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I run a particular program on linux which sometimes crashes. If you open it quickly after that, it listens on socket 49201 instead of 49200 as it did the first time. netstat reveals that 49200 is in a TIME_WAIT state.

Is there a program you can run to immediately force that socket move out of the TIME_WAIT state?

share|improve this question

migrated from stackoverflow.com Nov 10 '11 at 18:11

This question came from our site for professional and enthusiast programmers.

add comment

6 Answers

up vote 81 down vote accepted
/etc/init.d/networking restart

Let me elaborate. Transmission Control Protocol (TCP) is designed to be a bidirectional, ordered, and reliable data transmission protocol between two end points (programs). In this context, the term reliable means that it will retransmit the packets if it gets lost in the middle. TCP guarantees the reliability by sending back Acknowledgment (ACK) packets back for a single or a range of packets received from the peer.

This goes same for the control signals such as termination request/response. RFC 793 defines the TIME-WAIT state to be as follows:

TIME-WAIT - represents waiting for enough time to pass to be sure the remote TCP received the acknowledgment of its connection termination request.

See the following TCP state diagram: alt text

TCP is a bidirectional communication protocol, so when the connection is established, there is not a difference between the client and the server. Also, either one can call quits, and both peers needs to agree on closing to fully close an established TCP connection.

Let's call the first one to call the quits as the active closer, and the other peer the passive closer. When the active closer sends FIN, the state goes to FIN-WAIT-1. Then it receives an ACK for the sent FIN and the state goes to FIN-WAIT-2. Once it receives FIN also from the passive closer, the active closer sends the ACK to the FIN and the state goes to TIME-WAIT. In case the passive closer did not received the ACK to the second FIN, it will retransmit the FIN packet.

RFC 793 sets the TIME-OUT to be twice the Maximum Segment Lifetime, or 2MSL. Since MSL, the maximum time a packet can wander around Internet, is set to 2 minutes, 2MSL is 4 minutes. Since there is no ACK to an ACK, the active closer can't do anything but to wait 4 minutes if it adheres to the TCP/IP protocol correctly, just in case the passive sender has not received the ACK to its FIN (theoretically).

In reality, missing packets are probably rare, and very rare if it's all happening within the LAN or within a single machine.

To answer the question verbatim, How to forcibly close a socket in TIME_WAIT?, I will still stick to my original answer:

/etc/init.d/networking restart

Practically speaking, I would program it so it ignores TIME-WAIT state using SO_REUSEADDR option as WMR mentioned. What exactly does SO_REUSEADDR do?

This socket option tells the kernel that even if this port is busy (in
the TIME_WAIT state), go ahead and reuse it anyway. If it is busy, but with another state, you will still get an address already in use error. It is useful if your server has been shut down, and then restarted right away while sockets are still active on its port. You should be aware that if any unexpected data comes in, it may confuse your server, but while this is possible, it is not likely.

share|improve this answer
Great answer, but not the correct answer to his question. Restarting the networking would work, but then so would rebooting, so this can't be right. –  Chris Huang-Leaver Aug 6 '09 at 17:03
@Chris Huang-Leaver, the question is "Is there a program you can run to immediately force that socket move out of the TIME_WAIT state?" if rebooting could be considered running a program, then it too would be a right answer. Why do you think this can't be right? –  Eugene Yokota Aug 6 '09 at 17:59
WMR has the most useful answer (which is what I do when I run into this kind of issue). Restarting the network is too drastic to be solution, and could take longer than simply waiting for the timeout.The correct answer to his question is 'No', but SO won't let you type two letter answers :-) –  Chris Huang-Leaver Sep 2 '09 at 11:23
oh okay, next time some process hangs on SIGTERM I'll just smash my computer instead of fixing it. –  Longpoke May 13 '10 at 16:13
add comment

I don't know if you have the source code of that particular program you're running, but if so you could just set SO_REUSEADDR via setsockopt(2) which allows you to bind on the same local address even if the socket is in TIME_WAIT state (unless that socket is actively listening, see socket(7)).

For more information on the TIME_WAIT state see the Unix socket FAQ.

share|improve this answer
but i did not get the already bound error. when i execute the program again it listens in post(123456) also i can see a that the system is showing TIME_WAIT for that port but still i can connect. why? –  Jayapal Chandran Sep 27 '10 at 8:26
Even with SO_REUSEADDR, it is still possible to get the "Address already in use" error. For details, refer to hea-www.harvard.edu/~fine/Tech/addrinuse.html. –  Jingguo Yao Jun 27 '13 at 2:16
add comment

As far as I know there is no way to forcibly close the socket outside of writing a better signal handler into your program, but there is a /proc file which controls how long the timeout takes. The file is


and you can set the timeout to 1 second by doing this:

echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle

However, this page contains a warning about possible reliability issues when setting this variable.

There is also a related file


which controls whether TIME_WAIT sockets can be reused (presumably without any timeout).

Incidentally, the kernel documentation warns you not to change either of these values without 'advice/requests of technical experts'. Which I am not.

The program must have been written to attempt a binding to port 49200 and then increment by 1 if the port is already in use. Therefore, if you have control of the source code, you could change this behaviour to wait a few seconds and try again on the same port, instead of incrementing.

share|improve this answer
think the second two examples should be s/rw/tw/ I'd edit, but lack enough rep. –  blueshift Oct 6 '09 at 3:26
thanks blueshift, I have edited it now –  Leigh Caldwell Dec 11 '09 at 17:36
Taken from kernel documentation: Caution. Both tcp_tw_recycle and tcp_tw_reuse can cause problems. You should not enable either without understanding network topology in between node(s) which are using or used by the node where the parameter is enabled. Connections which go via nodes that are aware of TCP connection states, such as firewall, NAT or load balancer may start dropping frames because of the setting. The problem will become visible when there is large enough number of connections. –  jfarrell Feb 22 '10 at 18:54
add comment

Actually there is a way to kill a connection - killcx. They claim it works in any state of the connection (which I have not verified). You need to know the interface where communication happens though, it seems to assume eth0 by default.

UPDATE: another solution is cutter which comes in some linux distros' repositories.

share|improve this answer
Thanks! This utility works great! Saved me from having to restart a long job. –  Zanson Jul 19 '12 at 1:38
add comment

Another option is to use the SO_LINGER option with a timeout of 0. This way, when you close the socket is forcibly closed, sending a RST instead of going into the FIN/ACK closing behavior. This will avoid the TIME_WAIT state, and may be more appropriate for some uses.

share|improve this answer
It also loses any outbound data that is still in transit, and may cause an error at the other end. Not recommended. –  EJP Mar 31 '11 at 9:46
add comment

TIME_WAIT is most common issue in socket programming client server architecture. Wait for few seconds trying periodically is the best solution for it. For real time applications they need server must get up immediately There is SO_REUSEADDR option for them.

share|improve this answer
add comment

Your Answer


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.