Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix panic in Expiration Manager on atomic operations #3314

Merged
merged 4 commits into from
Sep 11, 2017
Merged
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
16 changes: 7 additions & 9 deletions vault/expiration.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ type ExpirationManager struct {
pending map[string]*time.Timer
pendingLock sync.RWMutex

tidyLock int64
tidyLock int32

// A set of locks to handle restoration
restoreMode int64
restoreMode int32
restoreModeLock sync.RWMutex
restoreRequestLock sync.RWMutex
restoreLocks []*locksutil.LockEntry
Expand All @@ -76,7 +75,6 @@ type ExpirationManager struct {
func NewExpirationManager(router *Router, view *BarrierView, ts *TokenStore, logger log.Logger) *ExpirationManager {
if logger == nil {
logger = log.New("expiration_manager")

}

exp := &ExpirationManager{
Expand Down Expand Up @@ -150,7 +148,7 @@ func (m *ExpirationManager) unlockLease(leaseID string) {

// inRestoreMode returns if we are currently in restore mode
func (m *ExpirationManager) inRestoreMode() bool {
return atomic.LoadInt64(&m.restoreMode) == 1
return atomic.LoadInt32(&m.restoreMode) == 1
}

// Tidy cleans up the dangling storage entries for leases. It scans the storage
Expand All @@ -166,12 +164,12 @@ func (m *ExpirationManager) Tidy() error {

var tidyErrors *multierror.Error

if !atomic.CompareAndSwapInt64(&m.tidyLock, 0, 1) {
if !atomic.CompareAndSwapInt32(&m.tidyLock, 0, 1) {
m.logger.Warn("expiration: tidy operation on leases is already in progress")
return fmt.Errorf("tidy operation on leases is already in progress")
}

defer atomic.CompareAndSwapInt64(&m.tidyLock, 1, 0)
defer atomic.CompareAndSwapInt32(&m.tidyLock, 1, 0)

m.logger.Info("expiration: beginning tidy operation on leases")
defer m.logger.Info("expiration: finished tidy operation on leases")
Expand Down Expand Up @@ -276,7 +274,7 @@ func (m *ExpirationManager) Restore(errorFunc func(), loadDelay time.Duration) (
// if restore mode finished successfully, restore mode was already
// disabled with the lock. In an error state, this will allow the
// Stop() function to shut everything down.
atomic.StoreInt64(&m.restoreMode, 0)
atomic.StoreInt32(&m.restoreMode, 0)

switch {
case retErr == nil:
Expand Down Expand Up @@ -391,7 +389,7 @@ func (m *ExpirationManager) Restore(errorFunc func(), loadDelay time.Duration) (
m.restoreModeLock.Lock()
m.restoreLoaded = sync.Map{}
m.restoreLocks = nil
atomic.StoreInt64(&m.restoreMode, 0)
atomic.StoreInt32(&m.restoreMode, 0)
m.restoreModeLock.Unlock()

m.logger.Info("expiration: lease restore complete")
Expand Down