Skip to content

Commit

Permalink
session: fix multiple domain creation racing in domap (pingcap#36792) (
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-srebot authored Aug 4, 2022
1 parent 341e65c commit a9e2b76
Showing 1 changed file with 6 additions and 18 deletions.
24 changes: 6 additions & 18 deletions session/tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,17 @@ import (
)

type domainMap struct {
mu sync.Mutex
domains map[string]*domain.Domain
mu sync.RWMutex
}

func (dm *domainMap) Get(store kv.Storage) (d *domain.Domain, err error) {
dm.mu.RLock()
key := store.UUID()

// If this is the only domain instance, and the caller doesn't provide store.
if len(dm.domains) == 1 && store == nil {
for _, r := range dm.domains {
dm.mu.RUnlock()
return r, nil
}
}
dm.mu.Lock()
defer dm.mu.Unlock()

key := store.UUID()
d = dm.domains[key]
dm.mu.RUnlock()
if d != nil {
return
}
Expand Down Expand Up @@ -94,7 +87,8 @@ func (dm *domainMap) Get(store kv.Storage) (d *domain.Domain, err error) {
if err != nil {
return nil, err
}
dm.Set(store, d)

dm.domains[key] = d

return
}
Expand All @@ -105,12 +99,6 @@ func (dm *domainMap) Delete(store kv.Storage) {
dm.mu.Unlock()
}

func (dm *domainMap) Set(store kv.Storage, domain *domain.Domain) {
dm.mu.Lock()
dm.domains[store.UUID()] = domain
dm.mu.Unlock()
}

var (
domap = &domainMap{
domains: map[string]*domain.Domain{},
Expand Down

0 comments on commit a9e2b76

Please sign in to comment.