diff --git a/pkg/discovery/p2p/discovery/disc_v4.go b/pkg/discovery/p2p/discovery/disc_v4.go index 7609d16a..9b13ca3a 100644 --- a/pkg/discovery/p2p/discovery/disc_v4.go +++ b/pkg/discovery/p2p/discovery/disc_v4.go @@ -303,9 +303,6 @@ func (d *DiscV4) OnNodeRecord(ctx context.Context, handler func(ctx context.Cont } func (l *ListenerV4) Close() error { - l.mu.Lock() - defer l.mu.Unlock() - if l.discovery != nil { l.discovery.Close() } @@ -315,8 +312,13 @@ func (l *ListenerV4) Close() error { l.localNode = nil } - if l.conn != nil { - return l.conn.Close() + l.mu.Lock() + conn := l.conn + l.conn = nil + l.mu.Unlock() + + if conn != nil { + return conn.Close() } return nil diff --git a/pkg/discovery/p2p/discovery/disc_v5.go b/pkg/discovery/p2p/discovery/disc_v5.go index e847d6ec..14e1d1df 100644 --- a/pkg/discovery/p2p/discovery/disc_v5.go +++ b/pkg/discovery/p2p/discovery/disc_v5.go @@ -307,9 +307,6 @@ func (d *DiscV5) OnNodeRecord(ctx context.Context, handler func(ctx context.Cont } func (l *ListenerV5) Close() error { - l.mu.Lock() - defer l.mu.Unlock() - if l.discovery != nil { l.discovery.Close() } @@ -319,8 +316,13 @@ func (l *ListenerV5) Close() error { l.localNode = nil } - if l.conn != nil { - return l.conn.Close() + l.mu.Lock() + conn := l.conn + l.conn = nil + l.mu.Unlock() + + if conn != nil { + return conn.Close() } return nil