proper cleanup neutrino database in case of error

This commit is contained in:
Roei Erez 2019-01-19 13:35:33 +02:00
parent 6a78141a2d
commit ff172bdd3a

@ -249,9 +249,14 @@ func newChainControlFromConfig(cfg *config, chanDB *channeldb.DB,
neutrino.BanDuration = 5 * time.Second neutrino.BanDuration = 5 * time.Second
svc, err := neutrino.NewChainService(config) svc, err := neutrino.NewChainService(config)
if err != nil { if err != nil {
nodeDatabase.Close()
return nil, nil, fmt.Errorf("unable to create neutrino: %v", err) return nil, nil, fmt.Errorf("unable to create neutrino: %v", err)
} }
svc.Start() svc.Start()
cleanUp = func() {
svc.Stop()
nodeDatabase.Close()
}
// Next we'll create the instances of the ChainNotifier and // Next we'll create the instances of the ChainNotifier and
// FilteredChainView interface which is backed by the neutrino // FilteredChainView interface which is backed by the neutrino
@ -259,6 +264,7 @@ func newChainControlFromConfig(cfg *config, chanDB *channeldb.DB,
cc.chainNotifier = neutrinonotify.New(svc, hintCache, hintCache) cc.chainNotifier = neutrinonotify.New(svc, hintCache, hintCache)
cc.chainView, err = chainview.NewCfFilteredChainView(svc) cc.chainView, err = chainview.NewCfFilteredChainView(svc)
if err != nil { if err != nil {
cleanUp()
return nil, nil, err return nil, nil, err
} }
@ -268,10 +274,6 @@ func newChainControlFromConfig(cfg *config, chanDB *channeldb.DB,
walletConfig.ChainSource = chain.NewNeutrinoClient( walletConfig.ChainSource = chain.NewNeutrinoClient(
activeNetParams.Params, svc, activeNetParams.Params, svc,
) )
cleanUp = func() {
svc.Stop()
nodeDatabase.Close()
}
case "bitcoind", "litecoind": case "bitcoind", "litecoind":
var bitcoindMode *bitcoindConfig var bitcoindMode *bitcoindConfig
switch { switch {
@ -495,6 +497,9 @@ func newChainControlFromConfig(cfg *config, chanDB *channeldb.DB,
wc, err := btcwallet.New(*walletConfig) wc, err := btcwallet.New(*walletConfig)
if err != nil { if err != nil {
fmt.Printf("unable to create wallet controller: %v\n", err) fmt.Printf("unable to create wallet controller: %v\n", err)
if cleanUp != nil {
cleanUp()
}
return nil, nil, err return nil, nil, err
} }
@ -530,10 +535,16 @@ func newChainControlFromConfig(cfg *config, chanDB *channeldb.DB,
lnWallet, err := lnwallet.NewLightningWallet(walletCfg) lnWallet, err := lnwallet.NewLightningWallet(walletCfg)
if err != nil { if err != nil {
fmt.Printf("unable to create wallet: %v\n", err) fmt.Printf("unable to create wallet: %v\n", err)
if cleanUp != nil {
cleanUp()
}
return nil, nil, err return nil, nil, err
} }
if err := lnWallet.Startup(); err != nil { if err := lnWallet.Startup(); err != nil {
fmt.Printf("unable to start wallet: %v\n", err) fmt.Printf("unable to start wallet: %v\n", err)
if cleanUp != nil {
cleanUp()
}
return nil, nil, err return nil, nil, err
} }