Skip to content

Commit

Permalink
peer: don't stop global msg router
Browse files Browse the repository at this point in the history
In this commit, we fix a bug that would cause a global message router to
be stopped anytime a peer disconnected. The global msg router only
allows `Start` to be called once, so afterwards, no messages would
properly be routed.
  • Loading branch information
Roasbeef committed Aug 15, 2024
1 parent 69716cd commit 5e27fd9
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions peer/brontide.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,11 @@ type Brontide struct {
// off new wire messages for handing.
msgRouter fn.Option[msgmux.Router]

// globalMsgRouter is a flag that indicates whether we have a global
// msg router. If so, then we don't worry about stopping the msg router
// when a peer disconnects.
globalMsgRouter bool

startReady chan struct{}
quit chan struct{}
wg sync.WaitGroup
Expand All @@ -547,6 +552,11 @@ var _ lnpeer.Peer = (*Brontide)(nil)
func NewBrontide(cfg Config) *Brontide {
logPrefix := fmt.Sprintf("Peer(%x):", cfg.PubKeyBytes)

// We have a global message router if one was passed in via the config.
// In this case, we don't need to attempt to tear it down when the peer
// is stopped.
globalMsgRouter := cfg.MsgRouter.IsSome()

// We'll either use the msg router instance passed in, or create a new
// blank instance.
msgRouter := cfg.MsgRouter.Alt(fn.Some[msgmux.Router](
Expand Down Expand Up @@ -576,6 +586,7 @@ func NewBrontide(cfg Config) *Brontide {
quit: make(chan struct{}),
log: build.NewPrefixLog(logPrefix, peerLog),
msgRouter: msgRouter,
globalMsgRouter: globalMsgRouter,
}

if cfg.Conn != nil && cfg.Conn.RemoteAddr() != nil {
Expand Down Expand Up @@ -1393,9 +1404,13 @@ func (p *Brontide) Disconnect(reason error) {

close(p.quit)

p.msgRouter.WhenSome(func(router msgmux.Router) {
router.Stop()
})
// If our msg router isn't global (local to this instance), then we'll
// stop it. Otherwise, we'll leave it running.
if !p.globalMsgRouter {
p.msgRouter.WhenSome(func(router msgmux.Router) {
router.Stop()
})
}
}

// String returns the string representation of this peer.
Expand Down

0 comments on commit 5e27fd9

Please sign in to comment.