Skip to content

Commit

Permalink
Merge pull request #2349 from halseth/autopilot-pilot-error-reset
Browse files Browse the repository at this point in the history
autopilot/manager: only set m.pilot if started successfully
  • Loading branch information
Roasbeef committed Dec 20, 2018
2 parents b5c3a37 + dbf7b38 commit 52f1a25
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions autopilot/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ func (m *Manager) StartAgent() error {

// Now that we have all the initial dependencies, we can create the
// auto-pilot instance itself.
m.pilot, err = New(*m.cfg.PilotCfg, initialChanState)
pilot, err := New(*m.cfg.PilotCfg, initialChanState)
if err != nil {
return err
}

if err := m.pilot.Start(); err != nil {
if err := pilot.Start(); err != nil {
return err
}

Expand All @@ -129,16 +129,18 @@ func (m *Manager) StartAgent() error {
// topology updates.
txnSubscription, err := m.cfg.SubscribeTransactions()
if err != nil {
defer m.pilot.Stop()
pilot.Stop()
return err
}
graphSubscription, err := m.cfg.SubscribeTopology()
if err != nil {
defer m.pilot.Stop()
defer txnSubscription.Cancel()
txnSubscription.Cancel()
pilot.Stop()
return err
}

m.pilot = pilot

// We'll launch a goroutine to provide the agent with notifications
// whenever the balance of the wallet changes.
// TODO(halseth): can lead to panic if in process of shutting down.
Expand All @@ -150,15 +152,15 @@ func (m *Manager) StartAgent() error {
for {
select {
case <-txnSubscription.ConfirmedTransactions():
m.pilot.OnBalanceChange()
pilot.OnBalanceChange()

// We won't act upon new unconfirmed transaction, as
// we'll only use confirmed outputs when funding.
// However, we will still drain this request in order
// to avoid goroutine leaks, and ensure we promptly
// read from the channel if available.
case <-txnSubscription.UnconfirmedTransactions():
case <-m.pilot.quit:
case <-pilot.quit:
return
case <-m.quit:
return
Expand Down Expand Up @@ -209,7 +211,7 @@ func (m *Manager) StartAgent() error {
Capacity: edgeUpdate.Capacity,
Node: chanNode,
}
m.pilot.OnChannelOpen(edge)
pilot.OnChannelOpen(edge)
}

// For each closed channel, we'll obtain
Expand All @@ -220,17 +222,17 @@ func (m *Manager) StartAgent() error {
chanClose.ChanID,
)

m.pilot.OnChannelClose(chanID)
pilot.OnChannelClose(chanID)
}

// If new nodes were added to the graph, or nod
// information has changed, we'll poke autopilot
// to see if it can make use of them.
if len(topChange.NodeUpdates) > 0 {
m.pilot.OnNodeUpdates()
pilot.OnNodeUpdates()
}

case <-m.pilot.quit:
case <-pilot.quit:
return
case <-m.quit:
return
Expand Down

0 comments on commit 52f1a25

Please sign in to comment.