client: drop an unresponsive client without waiting for the ping schedule

The client is dropped by the liveness monitor, and the monitor only reached its
decision after smpPingInterval (10 minutes) without a received transmission: the
timeout counter was read inside the branch that sends a ping, which is only taken
when the ping is due. Until then the requests kept timing out against a client
that could not recover, which is how delivery stays broken for ~20 minutes after
the path to the server dies with the socket still open - what happens on a
network change behind a SOCKS proxy, where the socket is connected to the proxy
and stays up.

The counter is now read on every pass, and the loop does not sleep past the next
request timeout, which is the only time the counter can change. The recovery
window becomes the time those timeouts take (smpPingCount * tcpTimeout), with a
90 second floor, instead of a fixed 15 minutes: reaching the counter already
requires that many requests to have timed out in sequence with nothing received,
so the window only has to cover them, and the floor keeps a low request timeout
from dropping a connection that is only slow.

Slowness alone still does not drop a client - any received transmission resets
both the counter and the time, so only silence for the whole window does.
This commit is contained in:
Narasimha-sc committed 2026-10-08 20:29:19 +00:00
1 parent e24d537eaa
commit 3882287e05
1 file changed
+13 -10
+13 -10
View File
@@ -675,19 +675,22 @@ getProtocolClient g nm transportSession@(_, srv, _) cfg@ProtocolClientConfig {qS
where
loop :: Int64 -> IO ()
loop delay = do
threadDelay' delay
-- the client is dropped here, so the loop cannot sleep past the next request timeout
threadDelay' $ if maxCnt > 0 then min delay requestTimeout else delay
diff <- diffUTCTime <$> getCurrentTime <*> readTVarIO lastReceived
let idle = diffToMicroseconds diff
remaining = smpPingInterval - idle
if remaining > 1_000_000 -- delay pings only for significant time
then loop remaining
else do
whenM (readTVarIO sendPings) $ void . runExceptT $ sendProtocolCommand c NRMBackground Nothing NoEntity (protocolPing @v @err @msg)
-- sendProtocolCommand/getResponse updates counter for each command
cnt <- readTVarIO timeoutErrorCount
-- drop client when maxCnt of commands have timed out in sequence, but only after some time has passed after last received response
when (maxCnt == 0 || cnt < maxCnt || diff < recoverWindow) $ loop smpPingInterval
recoverWindow = 15 * 60 -- seconds
due = remaining <= 1_000_000 -- delay pings only for significant time
when due $ whenM (readTVarIO sendPings) $ void . runExceptT $ sendProtocolCommand c NRMBackground Nothing NoEntity (protocolPing @v @err @msg)
-- sendProtocolCommand/getResponse updates counter for each command
cnt <- readTVarIO timeoutErrorCount
-- drop client when maxCnt of commands have timed out in sequence, but only after some time has passed after last received response
when (maxCnt == 0 || cnt < maxCnt || idle < recoverWindow) $ loop $ if due then smpPingInterval else remaining
-- the counter only changes when a request times out, so nothing is lost by checking it that often
requestTimeout = fromIntegral $ netTimeoutInt tcpTimeout NRMBackground
-- the counter requires maxCnt requests to time out, so the window only has to cover their timeouts,
-- with a floor, so that a low request timeout cannot drop a connection that is only slow
recoverWindow = max 90_000000 $ fromIntegral maxCnt * requestTimeout
maxCnt = smpPingCount networkConfig
process :: ProtocolClient v err msg -> IO ()