From c37b18cf8245834429a62894845af230f8708836 Mon Sep 17 00:00:00 2001 From: Gijs van Dam Date: Thu, 25 Sep 2025 14:58:02 +0200 Subject: [PATCH] chore: thread context through to SendCustomMessage The only way to unblock SendCustomMessage is if the peer activates, disconnects or the server shuts down. This means that if the context is cancelled, we will still wait until one of those other events happen. With this commit we thread the context through to SendCustomMessage, so that if the context is cancelled, we can return early. This improves the cancellation semantics. --- rpcserver.go | 4 ++-- server.go | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/rpcserver.go b/rpcserver.go index fd6743407..603856379 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -9251,7 +9251,7 @@ func (r *rpcServer) RegisterRPCMiddleware( } // SendCustomMessage sends a custom peer message. -func (r *rpcServer) SendCustomMessage(_ context.Context, +func (r *rpcServer) SendCustomMessage(ctx context.Context, req *lnrpc.SendCustomMessageRequest) (*lnrpc.SendCustomMessageResponse, error) { @@ -9261,7 +9261,7 @@ func (r *rpcServer) SendCustomMessage(_ context.Context, } err = r.server.SendCustomMessage( - peer, lnwire.MessageType(req.Type), req.Data, + ctx, peer, lnwire.MessageType(req.Type), req.Data, ) switch { case errors.Is(err, ErrPeerNotConnected): diff --git a/server.go b/server.go index b7d4f2370..6149db0ec 100644 --- a/server.go +++ b/server.go @@ -5263,21 +5263,24 @@ func (s *server) applyChannelUpdate(update *lnwire.ChannelUpdate1, // SendCustomMessage sends a custom message to the peer with the specified // pubkey. -func (s *server) SendCustomMessage(peerPub [33]byte, msgType lnwire.MessageType, - data []byte) error { +func (s *server) SendCustomMessage(ctx context.Context, peerPub [33]byte, + msgType lnwire.MessageType, data []byte) error { peer, err := s.FindPeerByPubStr(string(peerPub[:])) if err != nil { return err } - // We'll wait until the peer is active. + // We'll wait until the peer is active, but also listen for + // cancellation. select { case <-peer.ActiveSignal(): case <-peer.QuitSignal(): return fmt.Errorf("peer %x disconnected", peerPub) case <-s.quit: return ErrServerShuttingDown + case <-ctx.Done(): + return ctx.Err() } msg, err := lnwire.NewCustom(msgType, data)