Reputation: 15648
I have two processes written in two different programming languages that exchange information via a local TCP connection. One process is starting the other but their lifetime must be shared, i.e. if one dies, so does the other and vice versa.
I would like to use the TCP connection as the mechanism to detect if the other side is crashing, but I'm not sure if it's safe. I wonder if there are cases where the connection could be closed for other reasons than one of process crashing or exiting.
Edit:
Upvotes: 1
Views: 577
Reputation: 183
If the computers that are serving your processes are distant, then of course network problems along the way may cause problems, otherwise it's not trivial to close a connection. A tool such as "tcpkill" can close connections that originates or terminates on the local computer, and if you cross a firewall, then of course the firewall admin may close connections going through.
I think your scheme would work fairly well; if something causes the network connection to go down, then both your processes will terminate, so your worst case scenario would be excessive downtime - which I wouldn't expect from a connection through a normal network.
tcpkill: https://linux.die.net/man/8/tcpkill
Upvotes: 2
Reputation: 60117
If one side crashes (or closes/shuts-down its socket through any other means), the other one will see the socket as readable and get EOF on an attempted read.
You can inspect this behavior easily by observing a client-server nc pair, possibly with strace
.
Server:
strace nc -l localhost 3333
Client:
strace nc localhost 3333
Whenever one side is killed (e.g., with Ctrl+C
, Ctrl+\
or kill
), the other side gets EOF (== a read of 0
bytes) ASAP.
Upvotes: 0
Reputation: 158160
You can use iptables
to firewall the ports while the connection is running. By either ignoring packages or sending RSTs you could simulate different scenarios: remote host died, remote host closing the connection.
Examples:
# drop packages
iptables -p tcp --dport PORT_NUM -j DROP
# send RST
iptables -p tcp --dport PORT_NUM -j REJECT --reject-with tcp-reset
I've used the destination port above, to filter based on source port use --sport
Upvotes: 1