1

Normally when you pull the network cable out of a network device (which simulates some drop on the network, so an obscure disconnect), the underlaying sockets do not detect a disconnect (at least our units don't) so we use keep alive to drop connection and reconnect.

All units use hourly long poll to nginx server. So my question is:

If the unit does not detect a cable disconnect (and probably 100 other situations on the network), then also nginx does not disconnect the socket and that socket stays open?

Stay open for how long?

Should we use on the nginx side also tcp keep alive towards client? How?

1 Answer 1

2

You need TCP keepalive in order to detect outages like that. Socket is only an abstraction, and socket closes only when packet transmission in the connection fails.

In long poll situation, there is no packet transmission, so there is no packet loss event for TCP state machine to react to.

Therefore you need to enable TCP keepalive on the network stack:

sysctl -w net.ipv4.tcp_keepalive_time = 600
sysctl -w net.ipv4.tcp_keepalive_intvl = 60
sysctl -w net.ipv4.tcp_keepalive_probes = 5

First value specifies how long connection must be idle before a keepalive probe is sent.

Second value specifies how often the keepalive probe should be sent once sending is triggered.

Last value means how many probe packets must fail before the connection is considered broken.

The sysctl only modifies running configuration. You need to refer to your distribution's documentation to see how to make it persistent.

4
  • This is systemwide correct ? Can we make it only for nginx ?
    – Lonko
    Dec 23, 2020 at 10:35
  • 1
    I found this in the nginx documentation: so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt] and it says: configures the “TCP keepalive” behavior for the listening socket. If this parameter is omitted then the operating system’s settings will be in effect for the socket. If it is set to the value “on”, the SO_KEEPALIVE option is turned on for the socket. --- so the question is does this actually makes keepalive on the accepted socket passed from the tcp server listening socket. Or on the listening socket which i dont understand
    – Lonko
    Dec 23, 2020 at 10:44
  • True. serverfault.com/questions/181724/… has more information. Dec 23, 2020 at 10:45
  • I dont think so_keepalive works !? at least I dont see the drop of the open client socket... I disconnect the client by pulling the power plug and the active connection in nginx stays over the so_keepalive=1m:1:1 which is 1 min idle 1s interval and 1 failed probe
    – Lonko
    Dec 24, 2020 at 9:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .