From 1311fa2aad93930b3d3130150dff38f6cac7f852 Mon Sep 17 00:00:00 2001 From: Viktor Liu Date: Tue, 21 Apr 2026 14:54:07 +0200 Subject: [PATCH] netrelay: tighten watchdog tick for short idle timeouts Use min(idle/2, 50ms) so very short idle timeouts (mainly in tests) are caught within one tick; the 50ms cap still keeps detection latency bounded for long idle values without needlessly frequent wakeups. --- util/netrelay/relay.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/util/netrelay/relay.go b/util/netrelay/relay.go index a445d69be..137ac3875 100644 --- a/util/netrelay/relay.go +++ b/util/netrelay/relay.go @@ -146,7 +146,13 @@ func Relay(ctx context.Context, a, b io.ReadWriteCloser, opts Options) (aToB, bT // activity has been seen on either direction for idle. It exits as soon as // ctx is canceled so it doesn't outlive the relay. func watchdog(ctx context.Context, cancel context.CancelFunc, lastActivity *atomic.Int64, idleHit *atomic.Bool, idle time.Duration) { - tick := max(idle/2, 50*time.Millisecond) + // Cap the tick at 50ms so detection latency stays bounded regardless of + // how large idle is, and fall back to idle/2 when that is smaller so + // very short timeouts (mainly in tests) are still caught promptly. + tick := min(idle/2, 50*time.Millisecond) + if tick <= 0 { + tick = time.Millisecond + } t := time.NewTicker(tick) defer t.Stop() for {