diff --git a/lnd.go b/lnd.go index b05bf6e4..0561cdd3 100644 --- a/lnd.go +++ b/lnd.go @@ -35,7 +35,6 @@ import ( proxy "github.com/grpc-ecosystem/grpc-gateway/runtime" flags "github.com/jessevdk/go-flags" - "github.com/lightningnetwork/lnd/autopilot" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/keychain" "github.com/lightningnetwork/lnd/lncfg" @@ -44,6 +43,7 @@ import ( "github.com/lightningnetwork/lnd/lnwallet/btcwallet" "github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/macaroons" + "github.com/lightningnetwork/lnd/signal" "github.com/lightningnetwork/lnd/walletunlocker" "github.com/roasbeef/btcd/btcec" "github.com/roasbeef/btcd/wire" @@ -62,7 +62,6 @@ var ( Commit string cfg *config - shutdownChannel = make(chan struct{}) registeredChains = newChainRegistry() macaroonDatabaseDir string @@ -94,6 +93,8 @@ var ( // defers created in the top-level scope of a main method aren't executed if // os.Exit() is called. func lndMain() error { + defer ltndLog.Info("Shutdown complete") + // Load the configuration, and parse any command line options. This // function will also set up logging properly. loadedConfig, err := loadConfig() @@ -500,6 +501,7 @@ func lndMain() error { return err } server.fundingMgr = fundingMgr + defer fundingMgr.Stop() // Check macaroon authentication if macaroons aren't disabled. if macaroonService != nil { @@ -517,6 +519,7 @@ func lndMain() error { if err := rpcServer.Start(); err != nil { return err } + defer rpcServer.Stop() grpcServer := grpc.NewServer(serverOpts...) lnrpc.RegisterLightningServer(grpcServer, rpcServer) @@ -574,18 +577,9 @@ 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: + if !signal.Alive() { return nil - default: } synced, _, err := activeChainControl.wallet.IsSynced() @@ -615,10 +609,10 @@ func lndMain() error { srvrLog.Errorf("unable to start server: %v\n", err) return err } + defer server.Stop() // Now that the server has started, if the autopilot mode is currently // active, then we'll initialize a fresh instance of it and start it. - var pilot *autopilot.Agent if cfg.Autopilot.Active { pilot, err := initAutoPilot(server, cfg.Autopilot) if err != nil { @@ -631,21 +625,12 @@ func lndMain() error { err) return err } + defer pilot.Stop() } - addInterruptHandler(func() { - rpcServer.Stop() - fundingMgr.Stop() - if pilot != nil { - pilot.Stop() - } - server.Stop() - }) - // Wait for shutdown signal from either a graceful server stop or from // the interrupt handler. - <-shutdownChannel - ltndLog.Info("Shutdown complete") + <-signal.ShutdownChannel() return nil } @@ -1036,7 +1021,7 @@ func waitForWalletPassword(grpcEndpoints, restEndpoints []net.Addr, // Don't leave the file open in case the new wallet // could not be created for whatever reason. if err := loader.UnloadWallet(); err != nil { - ltndLog.Errorf("Could not unload new " + + ltndLog.Errorf("Could not unload new "+ "wallet: %v", err) } return nil, err @@ -1061,7 +1046,7 @@ func waitForWalletPassword(grpcEndpoints, restEndpoints []net.Addr, } return walletInitParams, nil - case <-shutdownChannel: + case <-signal.ShutdownChannel(): return nil, fmt.Errorf("shutting down") } }