signal: handle shutdown properly

This addresses issues related to the mobile lnd builds, where
calling `stopDaemon` and subsequently calling LndMobile's `Start`
results in crash.
This commit is contained in:
Hampus Sjöberg
2020-08-28 11:18:15 +02:00
parent 6046c5cf86
commit ada1bc701c
11 changed files with 170 additions and 129 deletions

View File

@@ -2,20 +2,21 @@ package build
import (
"github.com/btcsuite/btclog"
"github.com/lightningnetwork/lnd/signal"
)
// ShutdownLogger wraps an existing logger with a shutdown function which will
// be called on Critical/Criticalf to prompt shutdown.
type ShutdownLogger struct {
btclog.Logger
shutdown func()
}
// NewShutdownLogger creates a shutdown logger for the log provided which will
// use the signal package to request shutdown on critical errors.
func NewShutdownLogger(logger btclog.Logger) *ShutdownLogger {
func NewShutdownLogger(logger btclog.Logger, shutdown func()) *ShutdownLogger {
return &ShutdownLogger{
Logger: logger,
Logger: logger,
shutdown: shutdown,
}
}
@@ -26,6 +27,7 @@ func NewShutdownLogger(logger btclog.Logger) *ShutdownLogger {
// Note: it is part of the btclog.Logger interface.
func (s *ShutdownLogger) Criticalf(format string, params ...interface{}) {
s.Logger.Criticalf(format, params...)
s.Logger.Info("Sending request for shutdown")
s.shutdown()
}
@@ -36,18 +38,6 @@ func (s *ShutdownLogger) Criticalf(format string, params ...interface{}) {
// Note: it is part of the btclog.Logger interface.
func (s *ShutdownLogger) Critical(v ...interface{}) {
s.Logger.Critical(v)
s.Logger.Info("Sending request for shutdown")
s.shutdown()
}
// shutdown checks whether we are listening for interrupts, since a shutdown
// request to the signal package will block if it is not running, and requests
// shutdown if possible.
func (s *ShutdownLogger) shutdown() {
if !signal.Listening() {
s.Logger.Info("Request for shutdown ignored")
return
}
s.Logger.Info("Sending request for shutdown")
signal.RequestShutdown()
}