lnd: Call loader.UnloadWallet on shutdown

This is required to make restart work for LndMobile builds.
Not calling UnloadWallet would make `UnlockWallet` stall forever as
the file is already opened.
This commit is contained in:
Hampus Sjöberg 2020-09-03 18:10:30 +02:00
parent bc6e528887
commit 6dd1555e66
2 changed files with 16 additions and 0 deletions

11
lnd.go

@ -387,6 +387,11 @@ func Main(cfg *Config, lisCfg ListenerCfg, shutdownChan <-chan struct{}) error {
walletInitParams = *params walletInitParams = *params
privateWalletPw = walletInitParams.Password privateWalletPw = walletInitParams.Password
publicWalletPw = walletInitParams.Password publicWalletPw = walletInitParams.Password
defer func() {
if err := walletInitParams.UnloadWallet(); err != nil {
ltndLog.Errorf("Could not unload wallet: %v", err)
}
}()
if walletInitParams.RecoveryWindow > 0 { if walletInitParams.RecoveryWindow > 0 {
ltndLog.Infof("Wallet recovery mode enabled with "+ ltndLog.Infof("Wallet recovery mode enabled with "+
@ -944,6 +949,10 @@ type WalletUnlockParams struct {
// ChansToRestore a set of static channel backups that should be // ChansToRestore a set of static channel backups that should be
// restored before the main server instance starts up. // restored before the main server instance starts up.
ChansToRestore walletunlocker.ChannelsToRecover ChansToRestore walletunlocker.ChannelsToRecover
// UnloadWallet is a function for unloading the wallet, which should
// be called on shutdown.
UnloadWallet func() error
} }
// waitForWalletPassword will spin up gRPC and REST endpoints for the // waitForWalletPassword will spin up gRPC and REST endpoints for the
@ -1105,6 +1114,7 @@ func waitForWalletPassword(cfg *Config, restEndpoints []net.Addr,
RecoveryWindow: recoveryWindow, RecoveryWindow: recoveryWindow,
Wallet: newWallet, Wallet: newWallet,
ChansToRestore: initMsg.ChanBackups, ChansToRestore: initMsg.ChanBackups,
UnloadWallet: loader.UnloadWallet,
}, nil }, nil
// The wallet has already been created in the past, and is simply being // The wallet has already been created in the past, and is simply being
@ -1115,6 +1125,7 @@ func waitForWalletPassword(cfg *Config, restEndpoints []net.Addr,
RecoveryWindow: unlockMsg.RecoveryWindow, RecoveryWindow: unlockMsg.RecoveryWindow,
Wallet: unlockMsg.Wallet, Wallet: unlockMsg.Wallet,
ChansToRestore: unlockMsg.ChanBackups, ChansToRestore: unlockMsg.ChanBackups,
UnloadWallet: unlockMsg.UnloadWallet,
}, nil }, nil
case <-signal.ShutdownChannel(): case <-signal.ShutdownChannel():

@ -81,6 +81,10 @@ type WalletUnlockMsg struct {
// ChanBackups a set of static channel backups that should be received // ChanBackups a set of static channel backups that should be received
// after the wallet has been unlocked. // after the wallet has been unlocked.
ChanBackups ChannelsToRecover ChanBackups ChannelsToRecover
// UnloadWallet is a function for unloading the wallet, which should
// be called on shutdown.
UnloadWallet func() error
} }
// UnlockerService implements the WalletUnlocker service used to provide lnd // UnlockerService implements the WalletUnlocker service used to provide lnd
@ -346,6 +350,7 @@ func (u *UnlockerService) UnlockWallet(ctx context.Context,
Passphrase: password, Passphrase: password,
RecoveryWindow: recoveryWindow, RecoveryWindow: recoveryWindow,
Wallet: unlockedWallet, Wallet: unlockedWallet,
UnloadWallet: loader.UnloadWallet,
} }
// Before we return the unlock payload, we'll check if we can extract // Before we return the unlock payload, we'll check if we can extract