简体   繁体   中英

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:

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM