Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is any kind of keepalive necessary on localhost socket?

It is known fact that when one side of the connection crashes, it is not possible to detect that connection is lost. You have to set a keepalive on both ends on TCP level or on application protocol level.

References:

  • How to detect a TCP socket disconnection (with C Berkeley socket)
  • Detecting TCP Client Disconnect

But if the peers are on the same Linux machine and one of them segfaults, will the other one detect this situation just by receiving an error on the next read call?

All descriptors are closed, right? Shouldn't that close the connection correctly? (if something fails in networking, it is kernel fault and everyone is doomed anyways)

I'm trying to get this nice feature of the FIFOs: when you close one end, the other end receives an error/signal.

like image 494
Velkan Avatar asked Jun 19 '15 17:06

Velkan


1 Answers

If a process crashes the OS always closes its descriptors and sends FINs for TCP connection. If communication is established with remote peer there there are a lot of condition like OS crash or network when FIN is not sent or not delivered so the keep-alive mechanism is necessary.

In case of connection between peers on the same machine there are less possibilities how to kill one communication peer without informing the other peer but such possibilities still exist. For example firewall may drop the FIN packet:

$ telnet localhost
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

another terminal:

# iptables -A INPUT -p tcp --sport 23 -j DROP
# iptables -A INPUT -p tcp --sport 23 -j DROP
# kill -9 7737      # telnet client process

Voila - telnet server has no idea that client has terminated.

Well it is quite obscure condition and you may say that it never happen in your environment. But the keep-alive checks are implemented just because to solve an obscure and strange states.

like image 166
Zaboj Campula Avatar answered Oct 13 '22 14:10

Zaboj Campula