fix: only auto-remove disconnected pending sessions

This commit is contained in:
Alex P 2025-10-23 20:51:49 +03:00
parent 6898ede8e5
commit f7a5ed6d2e
1 changed files with 20 additions and 7 deletions

View File

@ -2,6 +2,8 @@ package kvm
import ( import (
"time" "time"
"github.com/pion/webrtc/v4"
) )
// emergencyPromotionContext holds context for emergency promotion attempts // emergencyPromotionContext holds context for emergency promotion attempts
@ -216,18 +218,29 @@ func (sm *SessionManager) promoteAfterGraceExpiration(expiredSessionID string, n
} }
} }
// handlePendingSessionTimeout removes timed-out pending sessions (DoS protection) // handlePendingSessionTimeout removes timed-out pending sessions only if disconnected
// Returns true if any pending session was removed // Connected pending sessions remain visible for approval (consistent UX)
// This prevents resource leaks while maintaining good user experience
func (sm *SessionManager) handlePendingSessionTimeout(now time.Time) bool { func (sm *SessionManager) handlePendingSessionTimeout(now time.Time) bool {
toDelete := make([]string, 0) toDelete := make([]string, 0)
for id, session := range sm.sessions { for id, session := range sm.sessions {
if session.Mode == SessionModePending && if session.Mode == SessionModePending &&
now.Sub(session.CreatedAt) > defaultPendingSessionTimeout { now.Sub(session.CreatedAt) > defaultPendingSessionTimeout {
websocketLogger.Debug(). // Only remove if the connection is closed/failed
Str("sessionId", id). // This prevents resource leaks while keeping connected sessions visible
Dur("age", now.Sub(session.CreatedAt)). if session.peerConnection != nil {
Msg("Removing timed-out pending session") connectionState := session.peerConnection.ConnectionState()
toDelete = append(toDelete, id) if connectionState == webrtc.PeerConnectionStateClosed ||
connectionState == webrtc.PeerConnectionStateFailed ||
connectionState == webrtc.PeerConnectionStateDisconnected {
websocketLogger.Debug().
Str("sessionId", id).
Dur("age", now.Sub(session.CreatedAt)).
Str("connectionState", connectionState.String()).
Msg("Removing timed-out disconnected pending session")
toDelete = append(toDelete, id)
}
}
} }
} }
for _, id := range toDelete { for _, id := range toDelete {