Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

peer+lnd: fix peer blocking on node shutdown #9275

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/release-notes/release-notes-0.19.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
* Make sure the RPC clients used to access the chain backend are [properly
shutdown](https://github.com/lightningnetwork/lnd/pull/9261).

* [Fixed a bug](https://github.com/lightningnetwork/lnd/pull/9275) where the
peer may block the shutdown process of lnd.

# New Features
## Functional Enhancements
## RPC Additions
Expand Down Expand Up @@ -196,5 +199,6 @@ The underlying functionality between those two options remain the same.
* Oliver Gugger
* Pins
* Viktor Tigerström
* Yong Yu
* Ziggie

16 changes: 12 additions & 4 deletions peer/brontide.go
Original file line number Diff line number Diff line change
Expand Up @@ -1467,10 +1467,18 @@ func (p *Brontide) Disconnect(reason error) {

// Make sure initialization has completed before we try to tear things
// down.
select {
case <-p.startReady:
case <-p.quit:
return
//
// NOTE: We only read the `startReady` chan if the peer has been
// started, otherwise we will skip reading it as this chan won't be
// closed, hence blocks forever.
if atomic.LoadInt32(&p.started) == 1 {
p.log.Debugf("Started, waiting on startReady signal")

select {
case <-p.startReady:
case <-p.quit:
return
}
}

err := fmt.Errorf("disconnecting %s, reason: %v", p, reason)
Expand Down
28 changes: 22 additions & 6 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,15 @@ type server struct {
// to handle dynamic IP changes.
lastDetectedIP net.IP

mu sync.RWMutex
mu sync.RWMutex

// peersByPub is a map of the active peers.
//
// NOTE: The key used here is the raw bytes of the peer's public key to
// string conversion, which means it cannot be printed using `%s` as it
// will just print the binary.
//
// TODO(yy): Use the hex string instead.
peersByPub map[string]*peer.Brontide

inboundPeers map[string]*peer.Brontide
Expand Down Expand Up @@ -4215,9 +4223,14 @@ func (s *server) addPeer(p *peer.Brontide) {
return
}

pubBytes := p.IdentityKey().SerializeCompressed()

// Ignore new peers if we're shutting down.
if s.Stopped() {
srvrLog.Infof("Server stopped, skipped adding peer=%x",
pubBytes)
p.Disconnect(ErrServerShuttingDown)

return
}

Expand All @@ -4226,8 +4239,9 @@ func (s *server) addPeer(p *peer.Brontide) {
// TODO(roasbeef): pipe all requests through to the
// queryHandler/peerManager

pubSer := p.IdentityKey().SerializeCompressed()
pubStr := string(pubSer)
// NOTE: This pubStr is a raw bytes to string conversion and will NOT
// be human-readable.
pubStr := string(pubBytes)

s.peersByPub[pubStr] = p

Expand All @@ -4240,7 +4254,7 @@ func (s *server) addPeer(p *peer.Brontide) {
// Inform the peer notifier of a peer online event so that it can be reported
// to clients listening for peer events.
var pubKey [33]byte
copy(pubKey[:], pubSer)
copy(pubKey[:], pubBytes)

s.peerNotifier.NotifyPeerOnline(pubKey)
}
Expand All @@ -4257,8 +4271,12 @@ func (s *server) addPeer(p *peer.Brontide) {
func (s *server) peerInitializer(p *peer.Brontide) {
defer s.wg.Done()

pubBytes := p.IdentityKey().SerializeCompressed()

// Avoid initializing peers while the server is exiting.
if s.Stopped() {
srvrLog.Infof("Server stopped, skipped initializing peer=%x",
pubBytes)
return
}

Expand All @@ -4276,8 +4294,6 @@ func (s *server) peerInitializer(p *peer.Brontide) {
s.wg.Add(1)
go s.peerTerminationWatcher(p, ready)

pubBytes := p.IdentityKey().SerializeCompressed()

// Start the peer! If an error occurs, we Disconnect the peer, which
// will unblock the peerTerminationWatcher.
if err := p.Start(); err != nil {
Expand Down
Loading