Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(server): fix deadlock caused by session close/delete #607

Merged
merged 2 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions server/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package server
import (
"context"
"fmt"
"io"
"log/slog"
"net/url"
"sync"
Expand All @@ -30,6 +31,7 @@ import (
// --- Session

type session struct {
io.Closer
sync.Mutex
id SessionId
clientIdentity string
Expand Down Expand Up @@ -75,7 +77,9 @@ func startSession(sessionId SessionId, sessionMetadata *proto.SessionMetadata, s
return s
}

func (s *session) closeChannels() {
func (s *session) Close() {
s.Lock()
defer s.Unlock()
s.cancel()
if s.heartbeatCh != nil {
close(s.heartbeatCh)
Expand All @@ -84,11 +88,6 @@ func (s *session) closeChannels() {
s.log.Debug("Session channels closed")
}

func (s *session) close() error {
s.log.Info("Session closing")
return s.delete()
}

func (s *session) delete() error {
// Delete ephemeral data associated with this session
sessionKey := SessionKey(s.id)
Expand Down Expand Up @@ -171,8 +170,7 @@ func (s *session) waitForHeartbeats() {
case <-timeoutCh:
s.log.Warn("Session expired")

s.Lock()
s.closeChannels()
s.Close()
err := s.delete()

if err != nil {
Expand All @@ -181,7 +179,6 @@ func (s *session) waitForHeartbeats() {
slog.Any("error", err),
)
}
s.Unlock()

s.sm.Lock()
s.sm.sessions.Remove(s.id)
Expand Down
12 changes: 5 additions & 7 deletions server/session_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,10 @@ func (sm *sessionManager) CloseSession(request *proto.CloseSessionRequest) (*pro
}
sm.sessions.Remove(s.id)
sm.Unlock()
s.Lock()
defer s.Unlock()
s.closeChannels()
err = s.close()

s.log.Info("Session closing")
s.Close()
err = s.delete()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -295,9 +295,7 @@ func (sm *sessionManager) Close() error {
sm.cancel()
for _, s := range sm.sessions.Values() {
sm.sessions.Remove(s.id)
s.Lock()
s.closeChannels()
s.Unlock()
s.Close()
}

sm.activeSessions.Unregister()
Expand Down
Loading