Skip to content

Commit 0e999ed

Browse files
authored
Merge pull request #9275 from yyforyongyu/fix-peer-shutdown
peer+lnd: fix peer blocking on node shutdown
2 parents bd36f76 + 3ab5669 commit 0e999ed

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

docs/release-notes/release-notes-0.19.0.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
* Make sure the RPC clients used to access the chain backend are [properly
5252
shutdown](https://github.com/lightningnetwork/lnd/pull/9261).
5353

54+
* [Fixed a bug](https://github.com/lightningnetwork/lnd/pull/9275) where the
55+
peer may block the shutdown process of lnd.
56+
5457
# New Features
5558
## Functional Enhancements
5659
## RPC Additions
@@ -196,5 +199,6 @@ The underlying functionality between those two options remain the same.
196199
* Oliver Gugger
197200
* Pins
198201
* Viktor Tigerström
202+
* Yong Yu
199203
* Ziggie
200204

peer/brontide.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,10 +1467,18 @@ func (p *Brontide) Disconnect(reason error) {
14671467

14681468
// Make sure initialization has completed before we try to tear things
14691469
// down.
1470-
select {
1471-
case <-p.startReady:
1472-
case <-p.quit:
1473-
return
1470+
//
1471+
// NOTE: We only read the `startReady` chan if the peer has been
1472+
// started, otherwise we will skip reading it as this chan won't be
1473+
// closed, hence blocks forever.
1474+
if atomic.LoadInt32(&p.started) == 1 {
1475+
p.log.Debugf("Started, waiting on startReady signal")
1476+
1477+
select {
1478+
case <-p.startReady:
1479+
case <-p.quit:
1480+
return
1481+
}
14741482
}
14751483

14761484
err := fmt.Errorf("disconnecting %s, reason: %v", p, reason)

server.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,15 @@ type server struct {
196196
// to handle dynamic IP changes.
197197
lastDetectedIP net.IP
198198

199-
mu sync.RWMutex
199+
mu sync.RWMutex
200+
201+
// peersByPub is a map of the active peers.
202+
//
203+
// NOTE: The key used here is the raw bytes of the peer's public key to
204+
// string conversion, which means it cannot be printed using `%s` as it
205+
// will just print the binary.
206+
//
207+
// TODO(yy): Use the hex string instead.
200208
peersByPub map[string]*peer.Brontide
201209

202210
inboundPeers map[string]*peer.Brontide
@@ -4215,9 +4223,14 @@ func (s *server) addPeer(p *peer.Brontide) {
42154223
return
42164224
}
42174225

4226+
pubBytes := p.IdentityKey().SerializeCompressed()
4227+
42184228
// Ignore new peers if we're shutting down.
42194229
if s.Stopped() {
4230+
srvrLog.Infof("Server stopped, skipped adding peer=%x",
4231+
pubBytes)
42204232
p.Disconnect(ErrServerShuttingDown)
4233+
42214234
return
42224235
}
42234236

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

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

42324246
s.peersByPub[pubStr] = p
42334247

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

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

4274+
pubBytes := p.IdentityKey().SerializeCompressed()
4275+
42604276
// Avoid initializing peers while the server is exiting.
42614277
if s.Stopped() {
4278+
srvrLog.Infof("Server stopped, skipped initializing peer=%x",
4279+
pubBytes)
42624280
return
42634281
}
42644282

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

4279-
pubBytes := p.IdentityKey().SerializeCompressed()
4280-
42814297
// Start the peer! If an error occurs, we Disconnect the peer, which
42824298
// will unblock the peerTerminationWatcher.
42834299
if err := p.Start(); err != nil {

0 commit comments

Comments
 (0)