Skip to content

Commit

Permalink
Use defer to unlock mutex (#66)
Browse files Browse the repository at this point in the history
* FIX: deadlock fix - mutex unlock via defer in GetOrSet

* FIX: move all mutex Unlocks into defer

* FIX: remove unnecessary new lines
  • Loading branch information
vlasashk authored Aug 29, 2024
1 parent ac0980a commit 0991f9b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 60 deletions.
6 changes: 2 additions & 4 deletions cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@ func (c *cleaner) start(r eremover, interval time.Duration) error {
return errors.New("imcache: interval must be greater than 0")
}
c.mu.Lock()
defer c.mu.Unlock()
if c.running {
c.mu.Unlock()
return errors.New("imcache: cleaner already running")
}
c.running = true
c.stopCh = make(chan struct{})
c.doneCh = make(chan struct{})
c.mu.Unlock()
go func() {
ticker := time.NewTicker(interval)
defer ticker.Stop()
Expand All @@ -56,13 +55,12 @@ func (c *cleaner) start(r eremover, interval time.Duration) error {

func (c *cleaner) stop() {
c.mu.Lock()
defer c.mu.Unlock()
if !c.running {
c.mu.Unlock()
return
}
c.running = false
close(c.stopCh)
// Wait for the cleaner goroutine to stop while holding the lock.
<-c.doneCh
c.mu.Unlock()
}
Loading

0 comments on commit 0991f9b

Please sign in to comment.