diff --git a/spanner/session.go b/spanner/session.go index 6c90a8f193e2..2948790e663d 100644 --- a/spanner/session.go +++ b/spanner/session.go @@ -1191,6 +1191,11 @@ func (p *sessionPool) incNumInUseLocked(ctx context.Context) { func (p *sessionPool) decNumInUseLocked(ctx context.Context) { p.numInUse-- + if int64(p.numInUse) < 0 { + // print whole call stack trace + logf(p.sc.logger, "Number of sessions in use is negative, resetting it to currSessionsCheckedOutLocked. Stack trace: %s", string(debug.Stack())) + p.numInUse = p.currSessionsCheckedOutLocked() + } p.recordStat(ctx, SessionsCount, int64(p.numInUse), tagNumInUseSessions) p.recordStat(ctx, ReleasedSessionsCount, 1) if p.otConfig != nil { @@ -1459,12 +1464,12 @@ func (hc *healthChecker) healthCheck(s *session) { defer hc.markDone(s) if !s.pool.isValid() { // Session pool is closed, perform a garbage collection. - s.destroy(false, true) + s.destroy(false, false) return } if err := s.ping(); isSessionNotFoundError(err) { // Ping failed, destroy the session. - s.destroy(false, true) + s.destroy(false, false) } }