From 180d0aeb96cf347c60ef138d9094e8703f20fc48 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Sun, 16 Oct 2016 15:37:42 -0700 Subject: [PATCH] htlcswitch: fix link deletion bug with multiple open channels MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit fixes a bug in the htlcSwitch’s logic to unregister links after a peer has signaled that a channel has been closed. This bug would arise when multiple channels were opened with a single peer, and any of the channels were attempted to be closed. The cause of the bug was that the slice reference within the map wasn’t previously updated with the re-slicing to truncate the (duplicated, unneeded) element from the slice. By updating the map’s reference directly, we fix this behavior. The safe handling of adding/removing links/interfaces between the htlcSwitch’s two goroutines has sprawled a bit, and can be hard to follow due to the map usage. In the future this section of the code will be cleaned up and the redundant indexes removed. --- htlcswitch.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/htlcswitch.go b/htlcswitch.go index 34a057f8..478b9fbd 100644 --- a/htlcswitch.go +++ b/htlcswitch.go @@ -506,9 +506,16 @@ func (h *htlcSwitch) handleUnregisterLink(req *unregisterLinkMsg) { if chanLink.chanPoint == req.chanPoint { chansRemoved = append(chansRemoved, req.chanPoint) + // We perform an in-place delete by sliding + // every element down one, then slicing off the + // last element. Additionally, we update the + // slice reference within the source map to + // ensure full deletion. copy(links[i:], links[i+1:]) links[len(links)-1] = nil - links = links[:len(links)-1] + h.interfaceMtx.Lock() + h.interfaces[chanInterface] = links[:len(links)-1] + h.interfaceMtx.Unlock() break }