Merge pull request #7744 from heyitsanthony/fix-auth-stop-race

auth: fix race on stopping simple token keeper
This commit is contained in:
Anthony Romano 2017-04-14 12:38:47 -07:00 committed by GitHub
commit e2d0db95eb

View File

@ -42,16 +42,18 @@ var (
type simpleTokenTTLKeeper struct { type simpleTokenTTLKeeper struct {
tokens map[string]time.Time tokens map[string]time.Time
stopCh chan chan struct{} donec chan struct{}
stopc chan struct{}
deleteTokenFunc func(string) deleteTokenFunc func(string)
mu *sync.Mutex mu *sync.Mutex
} }
func (tm *simpleTokenTTLKeeper) stop() { func (tm *simpleTokenTTLKeeper) stop() {
waitCh := make(chan struct{}) select {
tm.stopCh <- waitCh case tm.stopc <- struct{}{}:
<-waitCh case <-tm.donec:
close(tm.stopCh) }
<-tm.donec
} }
func (tm *simpleTokenTTLKeeper) addSimpleToken(token string) { func (tm *simpleTokenTTLKeeper) addSimpleToken(token string) {
@ -70,7 +72,10 @@ func (tm *simpleTokenTTLKeeper) deleteSimpleToken(token string) {
func (tm *simpleTokenTTLKeeper) run() { func (tm *simpleTokenTTLKeeper) run() {
tokenTicker := time.NewTicker(simpleTokenTTLResolution) tokenTicker := time.NewTicker(simpleTokenTTLResolution)
defer tokenTicker.Stop() defer func() {
tokenTicker.Stop()
close(tm.donec)
}()
for { for {
select { select {
case <-tokenTicker.C: case <-tokenTicker.C:
@ -83,9 +88,7 @@ func (tm *simpleTokenTTLKeeper) run() {
} }
} }
tm.mu.Unlock() tm.mu.Unlock()
case waitCh := <-tm.stopCh: case <-tm.stopc:
tm.tokens = make(map[string]time.Time)
waitCh <- struct{}{}
return return
} }
} }
@ -148,7 +151,8 @@ func (t *tokenSimple) enable() {
} }
t.simpleTokenKeeper = &simpleTokenTTLKeeper{ t.simpleTokenKeeper = &simpleTokenTTLKeeper{
tokens: make(map[string]time.Time), tokens: make(map[string]time.Time),
stopCh: make(chan chan struct{}), donec: make(chan struct{}),
stopc: make(chan struct{}),
deleteTokenFunc: delf, deleteTokenFunc: delf,
mu: &t.simpleTokensMu, mu: &t.simpleTokensMu,
} }
@ -156,13 +160,14 @@ func (t *tokenSimple) enable() {
} }
func (t *tokenSimple) disable() { func (t *tokenSimple) disable() {
if t.simpleTokenKeeper != nil {
t.simpleTokenKeeper.stop()
t.simpleTokenKeeper = nil
}
t.simpleTokensMu.Lock() t.simpleTokensMu.Lock()
tk := t.simpleTokenKeeper
t.simpleTokenKeeper = nil
t.simpleTokens = make(map[string]string) // invalidate all tokens t.simpleTokens = make(map[string]string) // invalidate all tokens
t.simpleTokensMu.Unlock() t.simpleTokensMu.Unlock()
if tk != nil {
tk.stop()
}
} }
func (t *tokenSimple) info(ctx context.Context, token string, revision uint64) (*AuthInfo, bool) { func (t *tokenSimple) info(ctx context.Context, token string, revision uint64) (*AuthInfo, bool) {