Skip to content
This repository has been archived by the owner on Jul 20, 2021. It is now read-only.

Commit

Permalink
Make the chance of a deadlock on gateway stop smaller
Browse files Browse the repository at this point in the history
  • Loading branch information
robvanmieghem committed Jan 9, 2020
1 parent 0d3ec2f commit 9449931
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
5 changes: 2 additions & 3 deletions modules/gateway/persist.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@ func (g *Gateway) threadedSaveLoop() {
defer g.threads.Done()

g.mu.Lock()
err = g.saveSync()
g.mu.Unlock()
if err != nil {
defer g.mu.Unlock()
if err := g.saveSync(); err != nil {
g.log.Println("ERROR: Unable to save gateway persist:", err)
}
}()
Expand Down
10 changes: 9 additions & 1 deletion sync/threadgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func (tg *ThreadGroup) isStopped() bool {

// Add increments the thread group counter.
func (tg *ThreadGroup) Add() error {
if tg.isStopped() {
return ErrStopped
}
tg.bmu.Lock()
defer tg.bmu.Unlock()

Expand Down Expand Up @@ -122,12 +125,17 @@ func (tg *ThreadGroup) Flush() error {
// order. After Stop is called, most actions will return ErrStopped.
func (tg *ThreadGroup) Stop() error {
// Establish that Stop has been called.

if tg.isStopped() {
return ErrStopped
}
tg.bmu.Lock()
defer tg.bmu.Unlock()

if tg.isStopped() {
return ErrStopped
}

close(tg.stopChan)

tg.mu.Lock()
Expand All @@ -136,12 +144,12 @@ func (tg *ThreadGroup) Stop() error {
}
tg.onStopFns = nil
tg.mu.Unlock()

tg.wg.Wait()

// After waiting for all resources to release the thread group, iterate
// through the stop functions and call them in reverse oreder.
tg.mu.Lock()

for i := len(tg.afterStopFns) - 1; i >= 0; i-- {
tg.afterStopFns[i]()
}
Expand Down

0 comments on commit 9449931

Please sign in to comment.