Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

signal: custom exit code status for interceptor #5636

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cmd/lnd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func main() {

// Load the configuration, and parse any command line options. This
// function will also set up logging properly.
loadedConfig, err := lnd.LoadConfig(shutdownInterceptor)
loadedConfig, err := lnd.LoadConfig(*shutdownInterceptor)
if err != nil {
if e, ok := err.(*flags.Error); !ok || e.Type != flags.ErrHelp {
// Print error if not due to help request.
Expand All @@ -34,9 +34,10 @@ func main() {
// Call the "real" main in a nested manner so the defers will properly
// be executed in the case of a graceful shutdown.
if err = lnd.Main(
loadedConfig, lnd.ListenerCfg{}, shutdownInterceptor,
loadedConfig, lnd.ListenerCfg{}, *shutdownInterceptor,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still dereferencing here?

); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
os.Exit(shutdownInterceptor.GetExitCode())
}
1 change: 0 additions & 1 deletion lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ func Main(cfg *Config, lisCfg ListenerCfg, interceptor signal.Interceptor) error
ltndLog.Errorf("Could not close log rotator: %v", err)
}
}()

// Show version at startup.
ltndLog.Infof("Version: %s commit=%s, build=%s, logging=%s, debuglevel=%s",
build.Version(), build.Commit, build.Deployment,
Expand Down
4 changes: 2 additions & 2 deletions mobile/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func Start(extraArgs string, rpcReady Callback) {

// Load the configuration, and parse the extra arguments as command
// line options. This function will also set up logging properly.
loadedConfig, err := lnd.LoadConfig(shutdownInterceptor)
loadedConfig, err := lnd.LoadConfig(*shutdownInterceptor)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we want to dereference here? Can't we just pass a pointer in?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially tried passing it directly to figure out the LoadConfig does not accpet and reference to Interceptor. I was not sure which all aspects of LND would be using the loadConfig as it seems very important to parse the user configs, hence i decided to go the safe way and passed a de-referenced interceptor

lnd/config.go

Line 569 in 9e8b9cc

func LoadConfig(interceptor signal.Interceptor) (*Config, error) {

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can bump LoadConfig to accept a pointer 👍

if err != nil {
atomic.StoreInt32(&lndStarted, 0)
_, _ = fmt.Fprintln(os.Stderr, err)
Expand Down Expand Up @@ -106,7 +106,7 @@ func Start(extraArgs string, rpcReady Callback) {
defer close(quit)

if err := lnd.Main(
loadedConfig, cfg, shutdownInterceptor,
loadedConfig, cfg, *shutdownInterceptor,
); err != nil {
if e, ok := err.(*flags.Error); ok &&
e.Type == flags.ErrHelp {
Expand Down
25 changes: 22 additions & 3 deletions signal/signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,24 @@ type Interceptor struct {

// quit is closed when instructing the main interrupt handler to exit.
quit chan struct{}

// exitCode is the exit code status when the main interrupt handler exits
exitCode chan int
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth adding a comment here that we must set a value here, otherwise the GetExitCode function will block.

}

// Intercept starts the interception of interrupt signals and returns an `Interceptor` instance.
// Note that any previous active interceptor must be stopped before a new one can be created
func Intercept() (Interceptor, error) {
func Intercept() (*Interceptor, error) {
if !atomic.CompareAndSwapInt32(&started, 0, 1) {
return Interceptor{}, errors.New("intercept already started")
return &Interceptor{}, errors.New("intercept already started")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can just return nil here, safe to assume that it won't be used when there's a non-nil error :)

}

channels := Interceptor{
interruptChannel: make(chan os.Signal, 1),
shutdownChannel: make(chan struct{}),
shutdownRequestChannel: make(chan struct{}),
quit: make(chan struct{}),
exitCode: make(chan int, 1),
}

signalsToCatch := []os.Signal{
Expand All @@ -59,7 +63,7 @@ func Intercept() (Interceptor, error) {
signal.Notify(channels.interruptChannel, signalsToCatch...)
go channels.mainInterruptHandler()

return channels, nil
return &channels, nil
}

// mainInterruptHandler listens for SIGINT (Ctrl+C) signals on the
Expand Down Expand Up @@ -95,16 +99,22 @@ func (c *Interceptor) mainInterruptHandler() {
select {
case signal := <-c.interruptChannel:
log.Infof("Received %v", signal)
// Adding exit status code 0 as this will quit after graceful shutdown
c.setExitCode(0)
shutdown()

case <-c.shutdownRequestChannel:
log.Infof("Received shutdown request.")
// Adding exit status code 0 as this will quit after graceful shutdown
c.setExitCode(1)
DarthBenro008 marked this conversation as resolved.
Show resolved Hide resolved
shutdown()

case <-c.quit:
log.Infof("Gracefully shutting down.")
close(c.shutdownChannel)
signal.Stop(c.interruptChannel)
// Adding exit status code 0 as this will quit after graceful shutdown
c.setExitCode(0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to set an exit code here? I think we can only hit this case if we've called shutdown in one of the other cases?

We can't send multiple values into the channel otherwise we'll end up blocked. I think it's worth adding a comment above <-c.quit that we only reach this case once shutdown has been called.

return
}
}
Expand Down Expand Up @@ -147,3 +157,12 @@ func (c *Interceptor) RequestShutdown() {
func (c *Interceptor) ShutdownChannel() <-chan struct{} {
return c.shutdownChannel
}

// GetExitCode fetches the exit code that has been set of the Interceptor
func (c *Interceptor) GetExitCode() int {
DarthBenro008 marked this conversation as resolved.
Show resolved Hide resolved
return <-c.exitCode
}

func (c *Interceptor) setExitCode(exitCode int) {
c.exitCode <- exitCode
}