Skip to content

Commit c9efc98

Browse files
committed
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.
1 parent db14322 commit c9efc98

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

rpcserver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9251,7 +9251,7 @@ func (r *rpcServer) RegisterRPCMiddleware(
92519251
}
92529252

92539253
// SendCustomMessage sends a custom peer message.
9254-
func (r *rpcServer) SendCustomMessage(_ context.Context,
9254+
func (r *rpcServer) SendCustomMessage(ctx context.Context,
92559255
req *lnrpc.SendCustomMessageRequest) (*lnrpc.SendCustomMessageResponse,
92569256
error) {
92579257

@@ -9261,7 +9261,7 @@ func (r *rpcServer) SendCustomMessage(_ context.Context,
92619261
}
92629262

92639263
err = r.server.SendCustomMessage(
9264-
peer, lnwire.MessageType(req.Type), req.Data,
9264+
ctx, peer, lnwire.MessageType(req.Type), req.Data,
92659265
)
92669266
switch {
92679267
case errors.Is(err, ErrPeerNotConnected):

server.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5291,21 +5291,24 @@ func (s *server) applyChannelUpdate(update *lnwire.ChannelUpdate1,
52915291

52925292
// SendCustomMessage sends a custom message to the peer with the specified
52935293
// pubkey.
5294-
func (s *server) SendCustomMessage(peerPub [33]byte, msgType lnwire.MessageType,
5295-
data []byte) error {
5294+
func (s *server) SendCustomMessage(ctx context.Context, peerPub [33]byte,
5295+
msgType lnwire.MessageType, data []byte) error {
52965296

52975297
peer, err := s.FindPeerByPubStr(string(peerPub[:]))
52985298
if err != nil {
52995299
return err
53005300
}
53015301

5302-
// We'll wait until the peer is active.
5302+
// We'll wait until the peer is active, but also listen for
5303+
// cancellation.
53035304
select {
53045305
case <-peer.ActiveSignal():
53055306
case <-peer.QuitSignal():
53065307
return fmt.Errorf("peer %x disconnected", peerPub)
53075308
case <-s.quit:
53085309
return ErrServerShuttingDown
5310+
case <-ctx.Done():
5311+
return ctx.Err()
53095312
}
53105313

53115314
msg, err := lnwire.NewCustom(msgType, data)

0 commit comments

Comments
 (0)