diff --git a/cln_plugin/cln_plugin.go b/cln_plugin/cln_plugin.go index 039fc9b..4ae8b4b 100644 --- a/cln_plugin/cln_plugin.go +++ b/cln_plugin/cln_plugin.go @@ -66,6 +66,10 @@ func (c *ClnPlugin) Start() { c.setupLogging() go c.listenRequests() <-c.done + s := c.server + if s != nil { + <-s.completed + } } // Stops the cln plugin. Drops any remaining work immediately. diff --git a/cln_plugin/cmd/main.go b/cln_plugin/cmd/main.go index 0fc934d..d358eb5 100644 --- a/cln_plugin/cmd/main.go +++ b/cln_plugin/cmd/main.go @@ -2,11 +2,20 @@ package main import ( "os" + "os/signal" + "syscall" "github.com/breez/lspd/cln_plugin" ) func main() { 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() } diff --git a/cln_plugin/server.go b/cln_plugin/server.go index e11a240..b03c97c 100644 --- a/cln_plugin/server.go +++ b/cln_plugin/server.go @@ -35,6 +35,7 @@ type server struct { newSubscriber chan struct{} started chan struct{} done chan struct{} + completed chan struct{} startError chan error sendQueue chan *htlcAcceptedMsg recvQueue chan *htlcResultMsg @@ -76,6 +77,7 @@ func (s *server) Start() error { } s.done = make(chan struct{}) + s.completed = make(chan struct{}) s.newSubscriber = make(chan struct{}) s.grpcServer = grpc.NewServer( grpc.KeepaliveParams(keepalive.ServerParameters{ @@ -93,7 +95,9 @@ func (s *server) Start() error { go s.listenHtlcRequests() go s.listenHtlcResponses() 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. @@ -119,6 +123,7 @@ func (s *server) Stop() { s.grpcServer = nil close(s.done) + <-s.completed log.Printf("Server stopped.") } diff --git a/go.mod b/go.mod index 65f21e4..e5af55e 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.19 require ( 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/btcec/v2 v2.2.1 github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 diff --git a/itest/cln_lspd_node.go b/itest/cln_lspd_node.go index 4080523..0221e84 100644 --- a/itest/cln_lspd_node.go +++ b/itest/cln_lspd_node.go @@ -8,6 +8,7 @@ import ( "os/exec" "path/filepath" "sync" + "syscall" "github.com/breez/lntest" lspd "github.com/breez/lspd/rpc" @@ -109,6 +110,7 @@ func (c *ClnLspNode) Start() { }) cmd := exec.CommandContext(c.harness.Ctx, c.lspBase.scriptFilePath) + cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} logFile, err := os.Create(c.logFilePath) if err != nil { lntest.PerformCleanup(cleanups) @@ -136,7 +138,7 @@ func (c *ClnLspNode) Start() { return nil } - proc.Kill() + syscall.Kill(-proc.Pid, syscall.SIGINT) log.Printf("About to wait for lspd to exit") status, err := proc.Wait() diff --git a/itest/lnd_lspd_node.go b/itest/lnd_lspd_node.go index 601a83a..e9c0d10 100644 --- a/itest/lnd_lspd_node.go +++ b/itest/lnd_lspd_node.go @@ -10,6 +10,7 @@ import ( "path/filepath" "strings" "sync" + "syscall" "github.com/breez/lntest" lspd "github.com/breez/lspd/rpc" @@ -132,6 +133,7 @@ func (c *LndLspNode) Start() { } cmd := exec.CommandContext(c.harness.Ctx, c.lspBase.scriptFilePath) + cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} logFile, err := os.Create(c.logFilePath) if err != nil { lntest.PerformCleanup(cleanups) @@ -159,7 +161,7 @@ func (c *LndLspNode) Start() { return nil } - proc.Kill() + syscall.Kill(-proc.Pid, syscall.SIGINT) log.Printf("About to wait for lspd to exit") status, err := proc.Wait()