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:
1 parent
6046c5cf86
commit
ada1bc701c
11 files changed
+170
-129
No files matched your search
+6
-16
@@ -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()
|
||||
}
|
||||
+7
-8
@@ -14,10 +14,6 @@ import (
|
||||
// RotatingLogWriter is a wrapper around the LogWriter that supports log file
|
||||
// rotation.
|
||||
type RotatingLogWriter struct {
|
||||
// GenSubLogger is a function that returns a new logger for a subsystem
|
||||
// belonging to the current RotatingLogWriter.
|
||||
GenSubLogger func(string) btclog.Logger
|
||||
|
||||
logWriter *LogWriter
|
||||
|
||||
backendLog *btclog.Backend
|
||||
@@ -39,16 +35,19 @@ func NewRotatingLogWriter() *RotatingLogWriter {
|
||||
logWriter := &LogWriter{}
|
||||
backendLog := btclog.NewBackend(logWriter)
|
||||
return &RotatingLogWriter{
|
||||
GenSubLogger: func(tag string) btclog.Logger {
|
||||
logger := backendLog.Logger(tag)
|
||||
return NewShutdownLogger(logger)
|
||||
},
|
||||
logWriter: logWriter,
|
||||
backendLog: backendLog,
|
||||
subsystemLoggers: SubLoggers{},
|
||||
}
|
||||
}
|
||||
|
||||
// GenSubLogger creates a new sublogger. A shutdown callback function
|
||||
// is provided to be able to shutdown in case of a critical error.
|
||||
func (r *RotatingLogWriter) GenSubLogger(tag string, shutdown func()) btclog.Logger {
|
||||
logger := r.backendLog.Logger(tag)
|
||||
return NewShutdownLogger(logger, shutdown)
|
||||
}
|
||||
|
||||
// RegisterSubLogger registers a new subsystem logger.
|
||||
func (r *RotatingLogWriter) RegisterSubLogger(subsystem string,
|
||||
logger btclog.Logger) {
|
||||
|
||||
Reference in new issue
Block a user