feat: add context support to resolve discv5 deadlock issue

(cherry picked from commit 14d13be8db633aa3b89b2cc529187c55a3461ab6)
This commit is contained in:
frank 2024-01-26 18:06:52 +08:00
parent 61c833f34d
commit 85f2c00b96
No known key found for this signature in database
GPG Key ID: B56FA1FC264D28FD
3 changed files with 9 additions and 6 deletions

2
.gitignore vendored
View File

@ -13,3 +13,5 @@
# Dependency directories (remove the comment below to include it)
# vendor/
.idea

View File

@ -123,8 +123,8 @@ type callTimeout struct {
}
// ListenV5 listens on the given connection.
func ListenV5(conn UDPConn, ln *enode.LocalNode, cfg Config) (*UDPv5, error) {
t, err := newUDPv5(conn, ln, cfg)
func ListenV5(ctx context.Context, conn UDPConn, ln *enode.LocalNode, cfg Config) (*UDPv5, error) {
t, err := newUDPv5(ctx, conn, ln, cfg)
if err != nil {
return nil, err
}
@ -136,8 +136,8 @@ func ListenV5(conn UDPConn, ln *enode.LocalNode, cfg Config) (*UDPv5, error) {
}
// newUDPv5 creates a UDPv5 transport, but doesn't start any goroutines.
func newUDPv5(conn UDPConn, ln *enode.LocalNode, cfg Config) (*UDPv5, error) {
closeCtx, cancelCloseCtx := context.WithCancel(context.Background())
func newUDPv5(ctx context.Context, conn UDPConn, ln *enode.LocalNode, cfg Config) (*UDPv5, error) {
closeCtx, cancelCloseCtx := context.WithCancel(ctx)
cfg = cfg.withDefaults()
t := &UDPv5{
// static fields

View File

@ -18,6 +18,7 @@ package discover
import (
"bytes"
"context"
"crypto/ecdsa"
"encoding/binary"
"fmt"
@ -93,7 +94,7 @@ func startLocalhostV5(t *testing.T, cfg Config) *UDPv5 {
realaddr := socket.LocalAddr().(*net.UDPAddr)
ln.SetStaticIP(realaddr.IP)
ln.Set(enr.UDP(realaddr.Port))
udp, err := ListenV5(socket, ln, cfg)
udp, err := ListenV5(context.Background(), socket, ln, cfg)
if err != nil {
t.Fatal(err)
}
@ -708,7 +709,7 @@ func newUDPV5Test(t *testing.T) *udpV5Test {
ln := enode.NewLocalNode(test.db, test.localkey)
ln.SetStaticIP(net.IP{10, 0, 0, 1})
ln.Set(enr.UDP(30303))
test.udp, _ = ListenV5(test.pipe, ln, Config{
test.udp, _ = ListenV5(context.Background(), test.pipe, ln, Config{
PrivateKey: test.localkey,
Log: testlog.Logger(t, log.LvlTrace),
ValidSchemes: enode.ValidSchemesForTesting,