Skip to content
Open
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
18 changes: 7 additions & 11 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ func NewAbstractManager(e *Engine) (*AbstractManager, error) {
}

func (a *AbstractManager) startMainLoop(preLoop func() error, receive func(r Reply) (UpdateStatus, error), preDestroy func()) {
errors := make(chan error)
preLoopFinished := make(chan bool)
preLoopError := make(chan error, 1) // 1 - to guarantee that preLoop won't hang on writing to the channel, if we exit the for/select loop earlier for some reason
preLoopFinished := make(chan struct{})

defer func() {
<-preLoopFinished // ensures preLoop goroutine has exited
Expand All @@ -110,21 +110,17 @@ func (a *AbstractManager) startMainLoop(preLoop func() error, receive func(r Rep

go a.eng.SubscribeState(a.engs)
go func() {
err := preLoop()
if err != nil {
errors <- err
}
close(errors)
errors = nil
preLoopFinished <- true
preLoopError <- preLoop()
close(preLoopFinished)
}()

for {
select {
case <-a.exit:
return
case e, ok := <-errors:
if ok {
case e := <-preLoopError:
preLoopError = nil // ignore listening on the channel
if e != nil {
a.err = e
return
}
Expand Down