handle clean shutdown

This commit is contained in:
Jesse de Wit
2023-01-23 14:54:55 +01:00
parent 294080b067
commit 5aad6b313e
6 changed files with 26 additions and 4 deletions

View File

@@ -66,6 +66,10 @@ func (c *ClnPlugin) Start() {
c.setupLogging() c.setupLogging()
go c.listenRequests() go c.listenRequests()
<-c.done <-c.done
s := c.server
if s != nil {
<-s.completed
}
} }
// Stops the cln plugin. Drops any remaining work immediately. // Stops the cln plugin. Drops any remaining work immediately.

View File

@@ -2,11 +2,20 @@ package main
import ( import (
"os" "os"
"os/signal"
"syscall"
"github.com/breez/lspd/cln_plugin" "github.com/breez/lspd/cln_plugin"
) )
func main() { func main() {
plugin := cln_plugin.NewClnPlugin(os.Stdin, os.Stdout) plugin := cln_plugin.NewClnPlugin(os.Stdin, os.Stdout)
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-c
// Stop everything gracefully on stop signal
plugin.Stop()
}()
plugin.Start() plugin.Start()
} }

View File

@@ -35,6 +35,7 @@ type server struct {
newSubscriber chan struct{} newSubscriber chan struct{}
started chan struct{} started chan struct{}
done chan struct{} done chan struct{}
completed chan struct{}
startError chan error startError chan error
sendQueue chan *htlcAcceptedMsg sendQueue chan *htlcAcceptedMsg
recvQueue chan *htlcResultMsg recvQueue chan *htlcResultMsg
@@ -76,6 +77,7 @@ func (s *server) Start() error {
} }
s.done = make(chan struct{}) s.done = make(chan struct{})
s.completed = make(chan struct{})
s.newSubscriber = make(chan struct{}) s.newSubscriber = make(chan struct{})
s.grpcServer = grpc.NewServer( s.grpcServer = grpc.NewServer(
grpc.KeepaliveParams(keepalive.ServerParameters{ grpc.KeepaliveParams(keepalive.ServerParameters{
@@ -93,7 +95,9 @@ func (s *server) Start() error {
go s.listenHtlcRequests() go s.listenHtlcRequests()
go s.listenHtlcResponses() go s.listenHtlcResponses()
close(s.started) close(s.started)
return s.grpcServer.Serve(lis) err = s.grpcServer.Serve(lis)
close(s.completed)
return err
} }
// Waits until the server has started, or errored during startup. // Waits until the server has started, or errored during startup.
@@ -119,6 +123,7 @@ func (s *server) Stop() {
s.grpcServer = nil s.grpcServer = nil
close(s.done) close(s.done)
<-s.completed
log.Printf("Server stopped.") log.Printf("Server stopped.")
} }

2
go.mod
View File

@@ -4,7 +4,7 @@ go 1.19
require ( require (
github.com/aws/aws-sdk-go v1.30.20 github.com/aws/aws-sdk-go v1.30.20
github.com/breez/lntest v0.0.17 github.com/breez/lntest v0.0.18
github.com/btcsuite/btcd v0.23.3 github.com/btcsuite/btcd v0.23.3
github.com/btcsuite/btcd/btcec/v2 v2.2.1 github.com/btcsuite/btcd/btcec/v2 v2.2.1
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1

View File

@@ -8,6 +8,7 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"sync" "sync"
"syscall"
"github.com/breez/lntest" "github.com/breez/lntest"
lspd "github.com/breez/lspd/rpc" lspd "github.com/breez/lspd/rpc"
@@ -109,6 +110,7 @@ func (c *ClnLspNode) Start() {
}) })
cmd := exec.CommandContext(c.harness.Ctx, c.lspBase.scriptFilePath) cmd := exec.CommandContext(c.harness.Ctx, c.lspBase.scriptFilePath)
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
logFile, err := os.Create(c.logFilePath) logFile, err := os.Create(c.logFilePath)
if err != nil { if err != nil {
lntest.PerformCleanup(cleanups) lntest.PerformCleanup(cleanups)
@@ -136,7 +138,7 @@ func (c *ClnLspNode) Start() {
return nil return nil
} }
proc.Kill() syscall.Kill(-proc.Pid, syscall.SIGINT)
log.Printf("About to wait for lspd to exit") log.Printf("About to wait for lspd to exit")
status, err := proc.Wait() status, err := proc.Wait()

View File

@@ -10,6 +10,7 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"sync" "sync"
"syscall"
"github.com/breez/lntest" "github.com/breez/lntest"
lspd "github.com/breez/lspd/rpc" lspd "github.com/breez/lspd/rpc"
@@ -132,6 +133,7 @@ func (c *LndLspNode) Start() {
} }
cmd := exec.CommandContext(c.harness.Ctx, c.lspBase.scriptFilePath) cmd := exec.CommandContext(c.harness.Ctx, c.lspBase.scriptFilePath)
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
logFile, err := os.Create(c.logFilePath) logFile, err := os.Create(c.logFilePath)
if err != nil { if err != nil {
lntest.PerformCleanup(cleanups) lntest.PerformCleanup(cleanups)
@@ -159,7 +161,7 @@ func (c *LndLspNode) Start() {
return nil return nil
} }
proc.Kill() syscall.Kill(-proc.Pid, syscall.SIGINT)
log.Printf("About to wait for lspd to exit") log.Printf("About to wait for lspd to exit")
status, err := proc.Wait() status, err := proc.Wait()