2 Commits

Author SHA1 Message Date
jar3b
ed59ea1d0a fix: add lock to terminate handler 2020-08-13 15:26:57 +03:00
jar3b
9eec782920 Fix terminate handler to prevent double channel close, up to 0.1.5 2019-06-18 23:16:44 +03:00
2 changed files with 13 additions and 6 deletions

View File

@@ -1,6 +1,9 @@
package grawt
import "sync"
type CloseHandler struct {
sync.Mutex
waiter *Waiter
Quit chan struct{}
active bool

View File

@@ -16,11 +16,11 @@ type Waiter struct {
func (w *Waiter) addHandler(f *func(), autoDone bool) *CloseHandler {
ch := CloseHandler{
w,
make(chan struct{}, 1),
true,
autoDone,
f,
waiter: w,
Quit: make(chan struct{}, 1),
active: true,
autoDone: autoDone,
handlerFunc: f,
}
w.waitGroup.Add(1)
w.closeHandlers = append(w.closeHandlers, &ch)
@@ -32,11 +32,15 @@ func (w *Waiter) terminateHandler(h *CloseHandler, forceWaitGroupDone bool) {
if h.handlerFunc != nil && *h.handlerFunc != nil {
(*h.handlerFunc)()
}
close(h.Quit)
h.Lock()
if h.active {
close(h.Quit)
}
if h.autoDone || forceWaitGroupDone {
w.waitGroup.Done()
}
h.active = false
h.Unlock()
}
func (w *Waiter) AddCloseHandler(f func(), waitForChannel bool) *CloseHandler {