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

lnd: add interrupt handler to handle shutdown requests while syncing chain backend #1276

Merged
Merged
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
19 changes: 14 additions & 5 deletions lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,20 @@ func lndMain() error {
ltndLog.Infof("Waiting for chain backend to finish sync, "+
"start_height=%v", bestHeight)

// We'll add an interrupt handler in order to process shutdown
// requests while the chain backend syncs.
addInterruptHandler(func() {
rpcServer.Stop()
fundingMgr.Stop()
})

for {
select {
case <-shutdownChannel:
return nil
default:
}

synced, _, err := activeChainControl.wallet.IsSynced()
if err != nil {
return err
Expand Down Expand Up @@ -617,16 +630,12 @@ func lndMain() error {
}

addInterruptHandler(func() {
ltndLog.Infof("Gracefully shutting down the server...")
rpcServer.Stop()
fundingMgr.Stop()
server.Stop()

if pilot != nil {
pilot.Stop()
}

server.WaitForShutdown()
server.Stop()
})

// Wait for shutdown signal from either a graceful server stop or from
Expand Down
5 changes: 0 additions & 5 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,11 +663,6 @@ func (s *server) Stopped() bool {
return atomic.LoadInt32(&s.shutdown) != 0
}

// WaitForShutdown blocks until all goroutines have been stopped.
func (s *server) WaitForShutdown() {
s.wg.Wait()
}

// initNetworkBootstrappers initializes a set of network peer bootstrappers
// based on the server, and currently active bootstrap mechanisms as defined
// within the current configuration.
Expand Down
6 changes: 2 additions & 4 deletions signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ func mainInterruptHandler() {
isShutdown = true
ltndLog.Infof("Shutting down...")

// Run handlers in LIFO order.
for i := range interruptCallbacks {
idx := len(interruptCallbacks) - 1 - i
callback := interruptCallbacks[idx]
// Execute the interrupt callbacks in FIFO order.
for _, callback := range interruptCallbacks {
Copy link
Member

Choose a reason for hiding this comment

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

What's the rationale behind the change to FIFO order from LIFO?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So that we can add and execute interrupt handlers as we go, making it work sort of like a defer.

Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW defers are executed in LIFO order.

callback()
}

Expand Down