From 620eaa319951237aef3e2990289f97804cbd0cda Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Thu, 30 Apr 2020 09:42:28 +0200 Subject: [PATCH] lnd+cmd: move interrupt into cmd If the main package is used as a library, we don't want it to register interrupt signals itself. Rather we want to pass in the shutdown channel manually. We do this in the cmd now. --- cmd/lnd/main.go | 11 +++++++++-- lnd.go | 14 ++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/cmd/lnd/main.go b/cmd/lnd/main.go index 42fab333..9364afbe 100644 --- a/cmd/lnd/main.go +++ b/cmd/lnd/main.go @@ -4,8 +4,9 @@ import ( "fmt" "os" - flags "github.com/jessevdk/go-flags" + "github.com/jessevdk/go-flags" "github.com/lightningnetwork/lnd" + "github.com/lightningnetwork/lnd/signal" ) func main() { @@ -17,9 +18,15 @@ func main() { os.Exit(1) } + // Hook interceptor for os signals. + signal.Intercept() + // Call the "real" main in a nested manner so the defers will properly // be executed in the case of a graceful shutdown. - if err := lnd.Main(loadedConfig, lnd.ListenerCfg{}); err != nil { + err = lnd.Main( + loadedConfig, lnd.ListenerCfg{}, signal.ShutdownChannel(), + ) + if err != nil { if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp { } else { _, _ = fmt.Fprintln(os.Stderr, err) diff --git a/lnd.go b/lnd.go index 9eb35bdd..edd0987b 100644 --- a/lnd.go +++ b/lnd.go @@ -147,13 +147,11 @@ type ListenerCfg struct { // listeners. type rpcListeners func() ([]*ListenerWithSignal, func(), error) -// Main is the true entry point for lnd. This function is required since defers -// created in the top-level scope of a main method aren't executed if os.Exit() -// is called. -func Main(config *Config, lisCfg ListenerCfg) error { - // Hook interceptor for os signals. - signal.Intercept() - +// Main is the true entry point for lnd. It accepts a fully populated and +// validated main configuration struct and an optional listener config struct. +// This function starts all main system components then blocks until a signal +// is received on the shutdownChan at which point everything is shut down again. +func Main(config *Config, lisCfg ListenerCfg, shutdownChan <-chan struct{}) error { cfg = config defer func() { ltndLog.Info("Shutdown complete") @@ -728,7 +726,7 @@ func Main(config *Config, lisCfg ListenerCfg) error { // Wait for shutdown signal from either a graceful server stop or from // the interrupt handler. - <-signal.ShutdownChannel() + <-shutdownChan return nil }