If the server does not receive the fourth wave request, will the server keep waiting?

2022.08.17
If the server does not receive the fourth wave request, will the server keep waiting?

If the server does not receive the fourth wave request, will the server keep waiting? The conclusion is that the server won't keep waiting for the fourth wave.

Carry an answer in a certain way, and write an article.

picturepicture

TCP waved four times

Under normal conditions. As long as the data is transmitted, whether it is the client or the server, it can actively initiate four waves to release the connection.

Just like the picture above, assuming that the four waves are initiated by the client, then it is the active party. The server passively receives the client's wave request, which is called the passive party.

The client and server, initially, are in the ESTABLISHED state.

  • The first wave: Under normal circumstances, the active party executes the close()​ or shutdown()​ method, and will send a FIN message, indicating "I no longer send data".
  • The second wave: After receiving the FIN message from the active party, the passive party immediately responded with an ACK, which means "I have received your FIN, and I know that you are no longer sending data".

The above mentioned is that the active party no longer sends data. But if at this time, the passive party still has data to send, it will continue to send. Note that although the passive party can send data to the active party between the second and third wave, it is not certain whether the active party can receive it normally or not. This will be discussed later.

  • The third wave: After the passive party perceives the second wave, it will do a series of finishing work, and finally call a close()​. At this time, the FIN-ACK of the third wave will be issued.
  • The fourth wave: the active party returns an ACK, which means it has been received.

Among them, the first wave and the third wave are actively triggered in the application (such as calling the close() method), which is where we usually need to pay attention to writing code.

The second and fourth wave are done automatically for us by the kernel protocol stack. We can't touch this place when we write code, so we don't need to care too much.

In addition, whether active or passive, each party sends a FIN​ and an ACK​ . Also received a FIN​ and an ACK .

Back to the main question.

If the server does not receive the fourth wave request, will the server keep waiting?

The fourth wave is triggered by the third wave. If the server does not receive the fourth wave, the server will think that the third wave is lost, so the server keeps retrying to send the third wave (FIN). The number of retransmissions is determined by the tcp_orphan_retries parameter of the system. control. The server disconnects the link directly after retrying for many times. So the conclusion is that the server won't keep waiting for the fourth wave.

picture

TCP lost the fourth wave

# cat /proc/sys/net/ipv4/tcp_orphan_retries
0
  • 1.
  • 2.

In addition, you will find that the tcp_orphan_retries parameter is 0, but it does not mean not to retry. When it is 0, the default value is 8. That is to retry 8 times.

/* Calculate maximal number or retries on an orphaned socket. */
static int tcp_orphan_retries(struct sock *sk, int alive)
{
    int retries = sysctl_tcp_orphan_retries; /* May be zero. */

    /* We know from an ICMP that something is wrong. */
    if (sk->sk_err_soft && !alive)
        retries = 0;

    /* However, if socket sent something recently, select some safe
     * number of retries. 8 corresponds to >100 seconds with minimal
     * RTO of 200msec. */
    if (retries == 0 && alive)
        retries = 8;
    return retries;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

Of course, if the server retries to send the FIN for the third time, it still starts a new client with the same port and IP. At this time, after the FIN retried by the server is received, the client will consider it as For abnormal data packets, send an RST directly to the server, and the connection between the two ends will also be disconnected.