Skip to content

Commit ff32e90

Browse files
authored
Merge pull request #10035 from ziggie1984/fix-switch-deadlock
fix switch deadlock
2 parents b3eb9a3 + edb7342 commit ff32e90

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

docs/release-notes/release-notes-0.19.2.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
could lead to a memory issues due to a goroutine leak in the peer/gossiper
3838
code.
3939

40+
- [Fixed](https://github.com/lightningnetwork/lnd/pull/10035) a deadlock (writer starvation) in the switch.
41+
4042
# New Features
4143

4244
## Functional Enhancements

htlcswitch/switch.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,6 @@ func (s *Switch) getLocalLink(pkt *htlcPacket, htlc *lnwire.UpdateAddHTLC) (
880880
// Try to find links by node destination.
881881
s.indexMtx.RLock()
882882
link, err := s.getLinkByShortID(pkt.outgoingChanID)
883-
defer s.indexMtx.RUnlock()
884883
if err != nil {
885884
// If the link was not found for the outgoingChanID, an outside
886885
// subsystem may be using the confirmed SCID of a zero-conf
@@ -892,6 +891,7 @@ func (s *Switch) getLocalLink(pkt *htlcPacket, htlc *lnwire.UpdateAddHTLC) (
892891
// do that upon receiving the packet.
893892
baseScid, ok := s.baseIndex[pkt.outgoingChanID]
894893
if !ok {
894+
s.indexMtx.RUnlock()
895895
log.Errorf("Link %v not found", pkt.outgoingChanID)
896896
return nil, NewLinkError(&lnwire.FailUnknownNextPeer{})
897897
}
@@ -900,10 +900,15 @@ func (s *Switch) getLocalLink(pkt *htlcPacket, htlc *lnwire.UpdateAddHTLC) (
900900
// link.
901901
link, err = s.getLinkByShortID(baseScid)
902902
if err != nil {
903+
s.indexMtx.RUnlock()
903904
log.Errorf("Link %v not found", baseScid)
904905
return nil, NewLinkError(&lnwire.FailUnknownNextPeer{})
905906
}
906907
}
908+
// We finished looking up the indexes, so we can unlock the mutex before
909+
// performing the link operations which might also acquire the lock
910+
// in case e.g. failAliasUpdate is called.
911+
s.indexMtx.RUnlock()
907912

908913
if !link.EligibleToForward() {
909914
log.Errorf("Link %v is not available to forward",
@@ -928,6 +933,7 @@ func (s *Switch) getLocalLink(pkt *htlcPacket, htlc *lnwire.UpdateAddHTLC) (
928933
"satisfied", pkt.outgoingChanID)
929934
return nil, htlcErr
930935
}
936+
931937
return link, nil
932938
}
933939

0 commit comments

Comments
 (0)