Skip to content

Commit

Permalink
Added mutex:es to map writes which were causing concurrent map writes…
Browse files Browse the repository at this point in the history
… under certain conditions
  • Loading branch information
oli committed Jan 27, 2025
1 parent bb4cd60 commit 5132112
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/gob"
"fmt"
"net/http"
"sync"
"time"
)

Expand Down Expand Up @@ -124,6 +125,7 @@ func GetRegistry(r *http.Request) *Registry {
type Registry struct {
request *http.Request
sessions map[string]sessionInfo
mu sync.RWMutex // Protects concurrent access to sessions map
}

// Get registers and returns a session for the given name and session store.
Expand All @@ -133,6 +135,10 @@ func (s *Registry) Get(store Store, name string) (session *Session, err error) {
if !isCookieNameValid(name) {
return nil, fmt.Errorf("sessions: invalid character in cookie name: %s", name)
}

s.mu.Lock()
defer s.mu.Unlock()

if info, ok := s.sessions[name]; ok {
session, err = info.s, info.e
} else {
Expand All @@ -147,6 +153,10 @@ func (s *Registry) Get(store Store, name string) (session *Session, err error) {
// Save saves all sessions registered for the current request.
func (s *Registry) Save(w http.ResponseWriter) error {
var errMulti MultiError

s.mu.RLock()
defer s.mu.RUnlock()

for name, info := range s.sessions {
session := info.s
if session.store == nil {
Expand Down

0 comments on commit 5132112

Please sign in to comment.