From b7c0e321d9acc970095c8a87745b8a872a69b750 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Mon, 23 Feb 2026 00:41:12 -0500 Subject: [PATCH] fix: move stale connection stat increments to detection site in RunPingTimerAsync --- src/NATS.Server/NatsClient.cs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/NATS.Server/NatsClient.cs b/src/NATS.Server/NatsClient.cs index b568536..daace96 100644 --- a/src/NATS.Server/NatsClient.cs +++ b/src/NATS.Server/NatsClient.cs @@ -642,6 +642,8 @@ public sealed class NatsClient : IDisposable if (Volatile.Read(ref _pingsOut) + 1 > _options.MaxPingsOut) { _logger.LogDebug("Client {ClientId} stale connection -- closing", Id); + Interlocked.Increment(ref _serverStats.StaleConnections); + Interlocked.Increment(ref _serverStats.StaleConnectionClients); await SendErrAndCloseAsync(NatsProtocol.ErrStaleConnection, ClientClosedReason.StaleConnection); return; } @@ -682,12 +684,6 @@ public sealed class NatsClient : IDisposable break; } - if (reason == ClientClosedReason.StaleConnection) - { - Interlocked.Increment(ref _serverStats.StaleConnections); - Interlocked.Increment(ref _serverStats.StaleConnectionClients); - } - _logger.LogDebug("Client {ClientId} connection closed: {CloseReason}", Id, reason); }