From 81e4160d67eca68bf780ad9bde613c86733fbc29 Mon Sep 17 00:00:00 2001 From: ziggie Date: Fri, 4 Jul 2025 12:21:03 +0200 Subject: [PATCH] switch: unlock mutex lock earlier We now unlock the mutex lock of the switch as soon as possible to avoid potetnial deadlock in the htlc switch. --- htlcswitch/switch.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/htlcswitch/switch.go b/htlcswitch/switch.go index 1009ba302..f89dbb4d2 100644 --- a/htlcswitch/switch.go +++ b/htlcswitch/switch.go @@ -880,7 +880,6 @@ func (s *Switch) getLocalLink(pkt *htlcPacket, htlc *lnwire.UpdateAddHTLC) ( // Try to find links by node destination. s.indexMtx.RLock() link, err := s.getLinkByShortID(pkt.outgoingChanID) - defer s.indexMtx.RUnlock() if err != nil { // If the link was not found for the outgoingChanID, an outside // subsystem may be using the confirmed SCID of a zero-conf @@ -892,6 +891,7 @@ func (s *Switch) getLocalLink(pkt *htlcPacket, htlc *lnwire.UpdateAddHTLC) ( // do that upon receiving the packet. baseScid, ok := s.baseIndex[pkt.outgoingChanID] if !ok { + s.indexMtx.RUnlock() log.Errorf("Link %v not found", pkt.outgoingChanID) return nil, NewLinkError(&lnwire.FailUnknownNextPeer{}) } @@ -900,10 +900,15 @@ func (s *Switch) getLocalLink(pkt *htlcPacket, htlc *lnwire.UpdateAddHTLC) ( // link. link, err = s.getLinkByShortID(baseScid) if err != nil { + s.indexMtx.RUnlock() log.Errorf("Link %v not found", baseScid) return nil, NewLinkError(&lnwire.FailUnknownNextPeer{}) } } + // We finished looking up the indexes, so we can unlock the mutex before + // performing the link operations which might also acquire the lock + // in case e.g. failAliasUpdate is called. + s.indexMtx.RUnlock() if !link.EligibleToForward() { log.Errorf("Link %v is not available to forward", @@ -928,6 +933,7 @@ func (s *Switch) getLocalLink(pkt *htlcPacket, htlc *lnwire.UpdateAddHTLC) ( "satisfied", pkt.outgoingChanID) return nil, htlcErr } + return link, nil }