From dbf7b380a902ff32282a6b3ac5695bcae92ffa58 Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Wed, 19 Dec 2018 14:54:56 +0100 Subject: [PATCH] autopilot/manager: only set m.pilot if started successfully This commit fixes a subtle bug within the autopilot manager, that would cause the active pilot to not be reset in case it wasn't started successfully. We also make sure the associated goroutines close over the started pilot, and not the active pilot. --- autopilot/manager.go | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/autopilot/manager.go b/autopilot/manager.go index 33182e98..9327d6e3 100644 --- a/autopilot/manager.go +++ b/autopilot/manager.go @@ -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 } @@ -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. @@ -150,7 +152,7 @@ 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. @@ -158,7 +160,7 @@ func (m *Manager) StartAgent() error { // 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 @@ -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 @@ -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