From 1d63421c48e300bb10efc7ab7aa759e61ce8db3a Mon Sep 17 00:00:00 2001 From: Andrew Lytvynov Date: Tue, 18 Aug 2020 17:11:19 -0700 Subject: [PATCH] Only decrement server_interactive_sessions_total once per session `session.Close` can get called multiple times, from different deferred cleanups. The associated metric decrement should only happen on the first call, to map 1:1 with increments. Without this, we could end up with negative `server_interactive_sessions_total` counts. Fixes https://github.com/gravitational/teleport/issues/3755 --- lib/srv/sess.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/srv/sess.go b/lib/srv/sess.go index d34c93a0088f5..2ec9eb598e7e1 100644 --- a/lib/srv/sess.go +++ b/lib/srv/sess.go @@ -49,7 +49,7 @@ const ( var ( serverSessions = prometheus.NewGauge( prometheus.GaugeOpts{ - Name: "server_interactive_sessions_total", + Name: teleport.MetricServerInteractiveSessions, Help: "Number of active sessions", }, ) @@ -563,8 +563,8 @@ func (s *session) PID() int { // Close ends the active session forcing all clients to disconnect and freeing all resources func (s *session) Close() error { - serverSessions.Dec() s.closeOnce.Do(func() { + serverSessions.Dec() // closing needs to happen asynchronously because the last client // (session writer) will try to close this session, causing a deadlock // because of closeOnce