Skip to content

Commit

Permalink
autopilot/agent: don't block Stop on pending connetion
Browse files Browse the repository at this point in the history
To ensure a call to ConnectToPeer doesn't block the agent from
shutting down, we'll launch it in a non-waitgrouped goroutine, that
will signal when a result is returned.
  • Loading branch information
halseth committed Feb 5, 2019
1 parent 077e188 commit c19763c
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion autopilot/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,40 @@ func (a *Agent) executeDirective(directive AttachmentDirective) {
return
}

alreadyConnected, err := a.cfg.ConnectToPeer(pub, directive.Addrs)
connected := make(chan bool)
errChan := make(chan error)

// To ensure a call to ConnectToPeer doesn't block the agent from
// shutting down, we'll launch it in a non-waitgrouped goroutine, that
// will signal when a result is returned.
// TODO(halseth): use DialContext to cancel on transport level.
go func() {
alreadyConnected, err := a.cfg.ConnectToPeer(
pub, directive.Addrs,
)
if err != nil {
select {
case errChan <- err:
case <-a.quit:
}
return
}

select {
case connected <- alreadyConnected:
case <-a.quit:
return
}
}()

var alreadyConnected bool
select {
case alreadyConnected = <-connected:
case err = <-errChan:
case <-a.quit:
return
}

if err != nil {
log.Warnf("Unable to connect to %x: %v",
pub.SerializeCompressed(), err)
Expand Down

0 comments on commit c19763c

Please sign in to comment.