From 4a200d28eba7268d7dec8445df62fbd316b94a0f Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Wed, 23 May 2018 02:55:30 -0400 Subject: [PATCH] lnd: add interrupt handler to handle shutdown requests while syncing chain backend --- lnd.go | 19 ++++++++++++++----- server.go | 5 ----- signal.go | 6 ++---- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/lnd.go b/lnd.go index 93e4905f..c908d978 100644 --- a/lnd.go +++ b/lnd.go @@ -570,7 +570,20 @@ func lndMain() error { ltndLog.Infof("Waiting for chain backend to finish sync, "+ "start_height=%v", bestHeight) + // We'll add an interrupt handler in order to process shutdown + // requests while the chain backend syncs. + addInterruptHandler(func() { + rpcServer.Stop() + fundingMgr.Stop() + }) + for { + select { + case <-shutdownChannel: + return nil + default: + } + synced, _, err := activeChainControl.wallet.IsSynced() if err != nil { return err @@ -617,16 +630,12 @@ func lndMain() error { } addInterruptHandler(func() { - ltndLog.Infof("Gracefully shutting down the server...") rpcServer.Stop() fundingMgr.Stop() - server.Stop() - if pilot != nil { pilot.Stop() } - - server.WaitForShutdown() + server.Stop() }) // Wait for shutdown signal from either a graceful server stop or from diff --git a/server.go b/server.go index 03480ae9..09f7b90a 100644 --- a/server.go +++ b/server.go @@ -663,11 +663,6 @@ func (s *server) Stopped() bool { return atomic.LoadInt32(&s.shutdown) != 0 } -// WaitForShutdown blocks until all goroutines have been stopped. -func (s *server) WaitForShutdown() { - s.wg.Wait() -} - // initNetworkBootstrappers initializes a set of network peer bootstrappers // based on the server, and currently active bootstrap mechanisms as defined // within the current configuration. diff --git a/signal.go b/signal.go index 5aa392ae..c409590b 100644 --- a/signal.go +++ b/signal.go @@ -48,10 +48,8 @@ func mainInterruptHandler() { isShutdown = true ltndLog.Infof("Shutting down...") - // Run handlers in LIFO order. - for i := range interruptCallbacks { - idx := len(interruptCallbacks) - 1 - i - callback := interruptCallbacks[idx] + // Execute the interrupt callbacks in FIFO order. + for _, callback := range interruptCallbacks { callback() }