I implemented a hello-world like client & echo server on Linux, and used tcpdump
to watch the packet exchanges between the two. The server forks a child process on each accepted connection, nothing fancy.
Once the child process serving the connection is killed, the server TCP socket goes into FIN-WAIT-2
(after sending a FIN
and receiving the ACK
), shown by command ss -tap
. ss
also shows it is orphaned since the Process column for this entry is empty.
Then I sent one more message from client, it triggers two more TCP messages:
- client push the message to server
- server respond with a
RST
and then ss
shows the server socket went missing, I assume it went back to CLOSED
state, and got reclaimed.
My question is this:
I can understand for an un-orphaned socket, FIN_WAIT_2
can serve the purpose of half-closed connection. But for an orphaned socket, what is the point? Why not go back to CLOSED
directly? I read from this post that FIN_WAIT_2
helps to prevent a future connection being mistakenly closed, but if that's the reason, then in my case, the server should NOT close the socket after receiving a regular message - it should wait forever until client sends a FIN
, correct?