From 017f86b8ab67210be4864bd6fa27401065a553e8 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Thu, 27 Apr 2023 09:03:46 +0200 Subject: [PATCH] quic: fix race condition when generating random holepunch packet --- p2p/transport/quic/transport.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/p2p/transport/quic/transport.go b/p2p/transport/quic/transport.go index 2d68b660c5..f279aed755 100644 --- a/p2p/transport/quic/transport.go +++ b/p2p/transport/quic/transport.go @@ -45,7 +45,9 @@ type transport struct { holePunchingMx sync.Mutex holePunching map[holePunchKey]*activeHolePunch - rnd rand.Rand + + rndMx sync.Mutex + rnd rand.Rand connMx sync.Mutex conns map[quic.Connection]*conn @@ -218,7 +220,10 @@ func (t *transport) holePunch(ctx context.Context, raddr ma.Multiaddr, p peer.ID var punchErr error loop: for i := 0; ; i++ { - if _, err := t.rnd.Read(payload); err != nil { + t.rndMx.Lock() + _, err := t.rnd.Read(payload) + t.rndMx.Unlock() + if err != nil { punchErr = err break }