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:
@@ -141,8 +141,14 @@ func TestSessionManagerAPIsAndClosePaths(t *testing.T) {
|
||||
_, _ = manager.NewSession("shell", "sid-2", "route-2", 80, 24)
|
||||
_, _ = manager.NewSession("shell", "sid-3", "route-3", 80, 24)
|
||||
manager.CloseAll()
|
||||
if s := manager.GetSession("sid-2"); s != nil && s.IsRunning() {
|
||||
t.Fatalf("CloseAll should stop session sid-2")
|
||||
if s := manager.GetSession("sid-2"); s != nil {
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -175,14 +175,13 @@ func (m *SessionManager) OnSessionEnd(sessionID string) {
|
||||
|
||||
func (m *SessionManager) CloseAll() {
|
||||
m.mu.RLock()
|
||||
sessions := make([]Session, 0, len(m.sessions))
|
||||
for _, session := range m.sessions {
|
||||
sessions = append(sessions, session)
|
||||
sessionIDs := make([]string, 0, len(m.sessions))
|
||||
for sessionID := range m.sessions {
|
||||
sessionIDs = append(sessionIDs, sessionID)
|
||||
}
|
||||
m.mu.RUnlock()
|
||||
for _, session := range sessions {
|
||||
_ = session.Close()
|
||||
_ = session.Wait()
|
||||
for _, sessionID := range sessionIDs {
|
||||
m.CloseSession(sessionID)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user