MCPcopy Create free account
hub / github.com/devnullvoid/pvetui / monitorSessionDisconnect

Method monitorSessionDisconnect

internal/vnc/session_manager.go:736–782  ·  view source on GitHub ↗

monitorSessionDisconnect monitors a session for client disconnections and handles immediate cleanup when clients disconnect.

(session *VNCSession)

Source from the content-addressed store, hash-verified

734// monitorSessionDisconnect monitors a session for client disconnections
735// and handles immediate cleanup when clients disconnect.
736func (sm *SessionManager) monitorSessionDisconnect(session *VNCSession) {
737 for {
738 select {
739 case <-session.disconnectChan:
740 // Client disconnected, consider cleanup after a grace period
741 if sm.logger != nil {
742 sm.logger.Info("Client disconnected from VNC session: session_id=%s, target_type=%s, target_name=%s, active_connections=%d",
743 session.ID, session.TargetType, session.TargetName, session.GetConnectionCount())
744 }
745
746 // Wait a short grace period to allow for reconnections
747 time.Sleep(5 * time.Second)
748
749 // Check if session is still disconnected and remove it
750 sm.mutex.Lock()
751 if existingSession, exists := sm.sessions[session.ID]; exists {
752 if existingSession.GetConnectionCount() == 0 && existingSession.State == SessionStateDisconnected {
753 if sm.logger != nil {
754 sm.logger.Info("Removing disconnected VNC session after grace period: session_id=%s, target_type=%s, target_name=%s",
755 session.ID, session.TargetType, session.TargetName)
756 }
757
758 // Shutdown the session
759 if err := existingSession.Shutdown(); err != nil && sm.logger != nil {
760 sm.logger.Error("Failed to shutdown disconnected session: session_id=%s, error=%v", session.ID, err)
761 }
762
763 // Remove from sessions map
764 delete(sm.sessions, session.ID)
765
766 // Free up the port
767 if existingSession.Port > 0 {
768 delete(sm.usedPorts, existingSession.Port)
769 }
770
771 // Notify callback of session count change
772 sm.notifySessionCountChange()
773 }
774 }
775 sm.mutex.Unlock()
776
777 case <-sm.ctx.Done():
778 // Session manager is shutting down
779 return
780 }
781 }
782}

Callers 1

Calls 5

GetConnectionCountMethod · 0.80
InfoMethod · 0.65
ShutdownMethod · 0.65
ErrorMethod · 0.65

Tested by

no test coverage detected