Repository: ethereum/go-ethereum (language: go)

Issue:
Flaky test
https://ci.appveyor.com/project/ethereum/go-ethereum/builds/50672070/job/3vwt3vsbmg40vmi8#L1114
```
panic: Log in goroutine after TestUDPv4_LookupIterator has completed: TRACE[09-25|09:29:15.777] << PONG/v4                                id=d89f4c51d8efdaf0 addr=127.0.255.3:5000 err="unsolicited reply"
	
goroutine 36067 [running]:
testing.(*common).logDepth(0xc000116820, {0xc0017a5200, 0x86}, 0x3)
	C:/Users/appveyor/AppData/Local/geth-go-1.23.1-windows-amd64/go/src/testing/testing.go:1029 +0x4c5
testing.(*common).log(...)
	C:/Users/appveyor/AppData/Local/geth-go-1.23.1-windows-amd64/go/src/testing/testing.go:1011
testing.(*common).Logf(0xc000116820, {0x7ff77015f66e?, 0x7ff770400cc0?}, {0xc00055f2a8?, 0xa?, 0xfffffffffffffff8?})
	C:/Users/appveyor/AppData/Local/geth-go-1.23.1-windows-amd64/go/src/testing/testing.go:1062 +0x54
github.com/ethereum/go-ethereum/internal/testlog.(*logger).flush(0xc000643d70)
	C:/projects/go-ethereum/internal/testlog/testlog.go:211 +0x16d
github.com/ethereum/go-ethereum/internal/testlog.(*logger).Trace(0xc000643d70, {0xc0017cc530, 0xa}, {0xc0017b56e0, 0x6, 0x6})
	C:/projects/go-ethereum/internal/testlog/testlog.go:121 +0xd2
github.com/ethereum/go-ethereum/p2p/discover.(*UDPv4).handlePacket(0xc000f902d0, {{{0x0, 0xffff7f00ff03}, {0xc000008048}}, 0x1388}, {0xc001792fc0?, 0x60?, 0xc0017b8c30?})
	C:/projects/go-ethereum/p2p/discover/v4_udp.go:576 +0x4bd
github.com/ethereum/go-ethereum/p2p/discover.(*udpTest).packetInFrom(0xc0000e2690, {0x0, 0x0}, 0xc000025650, {{{0x1df7cde9200?, 0xc00179bd30?}, {0xc000008048?}}, 0x10?}, {0x7ff7701f7270, 0xc0017b5620})
	C:/projects/go-ethereum/p2p/discover/v4_udp_test.go:104 +0x245
github.com/ethereum/go-ethereum/p2p/discover.serveTestnet.func1({0x7ff7701f7248, 0xc0016a3ea0?}, {{{0xc0016a3ea0?, 0xc00055f788?}, {0xc000008048?}}, 0x3ae0?}, {0xc001792ee0, 0x20, 0xe0})
	C:/projects/go-ethereum/p2p/discover/v4_lookup_test.go:143 +0x29e
reflect.Value.call({0x7ff7700f69a0?, 0xc0017bca80?, 0x7ff76fdd123f?}, {0x7ff77015fb0c, 0x4}, {0xc00055ff40, 0x3, 0xc0017bcb10?})
	C:/Users/appveyor/AppData/Local/geth-go-1.23.1-windows-amd64/go/src/reflect/value.go:581 +0xca6
reflect.Value.Call({0x7ff7700f69a0?, 0xc0017bca80?, 0xe0?}, {0xc00055ff40?, 0xcda35b3d79bcd714?, 0xb6253f6da5a12b72?})
	C:/Users/appveyor/AppData/Local/geth-go-1.23.1-windows-amd64/go/src/reflect/value.go:365 +0xb9
github.com/ethereum/go-ethereum/p2p/discover.(*udpTest).waitPacketOut(0xc0000e2690, {0x7ff7700f69a0, 0xc0017bca80})
	C:/projects/go-ethereum/p2p/discover/v4_udp_test.go:132 +0x579
github.com/ethereum/go-ethereum/p2p/discover.serveTestnet(0xc0000e2690, 0x7ff7700c1e80)
	C:/projects/go-ethereum/p2p/discover/v4_lookup_test.go:139 +0x3c
created by github.com/ethereum/go-ethereum/p2p/discover.TestUDPv4_LookupIterator in goroutine 211
	C:/projects/go-ethereum/p2p/discover/v4_lookup_test.go:78 +0x2b5
FAIL	github.com/ethereum/go-ethereum/p2p/discover	112.864s

```

Fix inbound... 



Discussion / hints:

Fix patch:
diff --git a/p2p/enode/nodedb.go b/p2p/enode/nodedb.go
index 1f31c98d2256..51e554e68adc 100644
--- a/p2p/enode/nodedb.go
+++ b/p2p/enode/nodedb.go
@@ -496,6 +496,10 @@ func nextNode(it iterator.Iterator) *Node {
 
 // Close flushes and closes the database files.
 func (db *DB) Close() {
-	close(db.quit)
+	select {
+	case <-db.quit: // already closed
+	default:
+		close(db.quit)
+	}
 	db.lvl.Close()
 }


Test patch:
diff --git a/p2p/discover/v4_lookup_test.go b/p2p/discover/v4_lookup_test.go
index 70bd7056fba2..29a9dd6645e0 100644
--- a/p2p/discover/v4_lookup_test.go
+++ b/p2p/discover/v4_lookup_test.go
@@ -21,6 +21,7 @@ import (
 	"fmt"
 	"net/netip"
 	"slices"
+	"sync"
 	"testing"
 
 	"github.com/ethereum/go-ethereum/crypto"
@@ -67,7 +68,11 @@ func TestUDPv4_Lookup(t *testing.T) {
 func TestUDPv4_LookupIterator(t *testing.T) {
 	t.Parallel()
 	test := newUDPTest(t)
-	defer test.close()
+	var wg sync.WaitGroup
+	defer func() {
+		test.close()
+		wg.Wait()
+	}()
 
 	// Seed table with initial nodes.
 	bootnodes := make([]*enode.Node, len(lookupTestnet.dists[256]))
@@ -75,7 +80,11 @@ func TestUDPv4_LookupIterator(t *testing.T) {
 		bootnodes[i] = lookupTestnet.node(256, i)
 	}
 	fillTable(test.table, bootnodes, true)
-	go serveTestnet(test, lookupTestnet)
+	wg.Add(1)
+	go func() {
+		serveTestnet(test, lookupTestnet)
+		wg.Done()
+	}()
 
 	// Create the iterator and collect the nodes it yields.
 	iter := test.udp.RandomNodes()
@@ -102,7 +111,11 @@ func TestUDPv4_LookupIterator(t *testing.T) {
 func TestUDPv4_LookupIteratorClose(t *testing.T) {
 	t.Parallel()
 	test := newUDPTest(t)
-	defer test.close()
+	var wg sync.WaitGroup
+	defer func() {
+		test.close()
+		wg.Wait()
+	}()
 
 	// Seed table with initial nodes.
 	bootnodes := make([]*enode.Node, len(lookupTestnet.dists[256]))
@@ -110,7 +123,12 @@ func TestUDPv4_LookupIteratorClose(t *testing.T) {
 		bootnodes[i] = lookupTestnet.node(256, i)
 	}
 	fillTable(test.table, bootnodes, true)
-	go serveTestnet(test, lookupTestnet)
+
+	wg.Add(1)
+	go func() {
+		serveTestnet(test, lookupTestnet)
+		wg.Done()
+	}()
 
 	it := test.udp.RandomNodes()
 	if ok := it.Next(); !ok || it.Node() == nil {