go: harden session cleanup path

Ensure SessionManager.CloseAll fully removes session and route mappings by reusing CloseSession for each tracked session ID.

Tighten coverage test assertions to verify CloseAll removes both sessions and route-key mappings, guarding against stale state regressions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
GitHub Copilot
2026-02-14 18:26:43 +00:00
parent 4e90b65c6b
commit c8040e5938
2 changed files with 13 additions and 8 deletions
+8 -2
View File
@@ -141,8 +141,14 @@ func TestSessionManagerAPIsAndClosePaths(t *testing.T) {
_, _ = manager.NewSession("shell", "sid-2", "route-2", 80, 24) _, _ = manager.NewSession("shell", "sid-2", "route-2", 80, 24)
_, _ = manager.NewSession("shell", "sid-3", "route-3", 80, 24) _, _ = manager.NewSession("shell", "sid-3", "route-3", 80, 24)
manager.CloseAll() manager.CloseAll()
if s := manager.GetSession("sid-2"); s != nil && s.IsRunning() { if s := manager.GetSession("sid-2"); s != nil {
t.Fatalf("CloseAll should stop session sid-2") t.Fatalf("CloseAll should remove session sid-2")
}
if s := manager.GetSession("sid-3"); s != nil {
t.Fatalf("CloseAll should remove session sid-3")
}
if manager.GetSessionByRouteKey("route-2") != nil || manager.GetSessionByRouteKey("route-3") != nil {
t.Fatalf("CloseAll should remove route mappings")
} }
} }
+5 -6
View File
@@ -175,14 +175,13 @@ func (m *SessionManager) OnSessionEnd(sessionID string) {
func (m *SessionManager) CloseAll() { func (m *SessionManager) CloseAll() {
m.mu.RLock() m.mu.RLock()
sessions := make([]Session, 0, len(m.sessions)) sessionIDs := make([]string, 0, len(m.sessions))
for _, session := range m.sessions { for sessionID := range m.sessions {
sessions = append(sessions, session) sessionIDs = append(sessionIDs, sessionID)
} }
m.mu.RUnlock() m.mu.RUnlock()
for _, session := range sessions { for _, sessionID := range sessionIDs {
_ = session.Close() m.CloseSession(sessionID)
_ = session.Wait()
} }
} }