From 0cbb228368b2e67332790d03b2a12ee34c741b88 Mon Sep 17 00:00:00 2001 From: rehs0y Date: Tue, 25 Aug 2020 14:29:34 +0000 Subject: [PATCH] fixed panic bug on chace eviction. added scenario to test. (#2125) ## Motivation When udp message from a new peer would arrive and the udp session state cache was full we try to evict one session. if one of the sessions in the state is older than 24hours we remove it first. this code path was never tested and had called `Close` on the connection after removing it from the map. Closes # ## Changes added test and fixed. ## Test Plan ## TODO - [x] Explain motivation or link existing issue(s) - [x] Test changes and document test plan - [x] Update documentation as needed --- p2p/net/udp.go | 2 +- p2p/net/udp_test.go | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/p2p/net/udp.go b/p2p/net/udp.go index 89ce405302..86c394f0b0 100644 --- a/p2p/net/udp.go +++ b/p2p/net/udp.go @@ -278,8 +278,8 @@ func (n *UDPNet) addConn(addr net.Addr, ucw udpConn) { lastk = k if time.Since(c.Created()) > maxUDPLife { delete(n.incomingConn, k) + c.Close() evicted = true - n.incomingConn[k].Close() break } diff --git a/p2p/net/udp_test.go b/p2p/net/udp_test.go index 7245ca6e03..06e7edecef 100644 --- a/p2p/net/udp_test.go +++ b/p2p/net/udp_test.go @@ -222,7 +222,7 @@ func TestUDPNet_Cache(t *testing.T) { }}) require.Len(t, n.incomingConn, 1) - for i := 1; i < maxUDPConn; i++ { + for i := 1; i < maxUDPConn-1; i++ { addrx := testUDPAddr() _, ok := n.incomingConn[addrx.String()] for ok { @@ -234,7 +234,7 @@ func TestUDPNet_Cache(t *testing.T) { }}) } - require.Len(t, n.incomingConn, maxUDPConn) + require.Len(t, n.incomingConn, maxUDPConn-1) addrx := testUDPAddr() _, ok := n.incomingConn[addrx.String()] @@ -242,9 +242,23 @@ func TestUDPNet_Cache(t *testing.T) { addrx = testUDPAddr() _, ok = n.incomingConn[addrx.String()] } + n.addConn(addrx, &udpConnMock{CreatedFunc: func() time.Time { + return time.Now().Add(-maxUDPLife - 1*time.Second) + }}) + + require.Len(t, n.incomingConn, maxUDPConn) + + addrx2 := testUDPAddr() + _, ok2 := n.incomingConn[addrx2.String()] + for ok2 { + addrx2 = testUDPAddr() + _, ok = n.incomingConn[addrx2.String()] + } + n.addConn(addrx2, &udpConnMock{CreatedFunc: func() time.Time { return time.Now() }}) + require.Len(t, n.incomingConn, maxUDPConn) i := 0