htlcswitch: fix link deletion bug with multiple open channels

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.
This commit is contained in:
Olaoluwa Osuntokun 2016-10-16 15:37:42 -07:00
parent e515710a7d
commit 180d0aeb96
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2

View File

@ -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
}