From 01b85dc70b59b472692cb1efd1f6fe5eb8f8567f Mon Sep 17 00:00:00 2001 From: Noah Levenson Date: Wed, 19 Jul 2023 12:48:44 -0700 Subject: [PATCH 1/3] fix data race --- nats/discover.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/nats/discover.go b/nats/discover.go index 64b1d25..752064f 100644 --- a/nats/discover.go +++ b/nats/discover.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "net" + "sync" "github.com/pion/logging" "github.com/pion/stun" @@ -60,6 +61,7 @@ type NATS struct { verbose bool net *vnet.Net dfErr error // filled by discoverFilteringBehavior + mu sync.Mutex } // NewNATS creats a new instance of NATS. @@ -329,13 +331,17 @@ func (nats *NATS) performTransactionWith(c *turn.Client, changeIP, changePort bo from := res.From.(*net.UDPAddr) if changeIP { if from.IP.Equal(c.STUNServerAddr().(*net.UDPAddr).IP) { + nats.mu.Lock() nats.dfErr = fmt.Errorf("CHANGE-REQUEST ignored (IP)") + nats.mu.Unlock() receivedCh <- false } } if changePort { if from.Port == c.STUNServerAddr().(*net.UDPAddr).Port { + nats.mu.Lock() nats.dfErr = fmt.Errorf("CHANGE-REQUEST ignored (Port)") + nats.mu.Unlock() receivedCh <- false } } From b7ef97adb292f4b98ca2da99de08a35ee7ca72d5 Mon Sep 17 00:00:00 2001 From: Noah Levenson Date: Wed, 19 Jul 2023 19:17:21 -0700 Subject: [PATCH 2/3] fix goroutine leak --- nats/discover.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/nats/discover.go b/nats/discover.go index 752064f..bcbd8be 100644 --- a/nats/discover.go +++ b/nats/discover.go @@ -335,6 +335,7 @@ func (nats *NATS) performTransactionWith(c *turn.Client, changeIP, changePort bo nats.dfErr = fmt.Errorf("CHANGE-REQUEST ignored (IP)") nats.mu.Unlock() receivedCh <- false + return } } if changePort { @@ -343,6 +344,7 @@ func (nats *NATS) performTransactionWith(c *turn.Client, changeIP, changePort bo nats.dfErr = fmt.Errorf("CHANGE-REQUEST ignored (Port)") nats.mu.Unlock() receivedCh <- false + return } } From 9c44bfce34b567b46d6cb4387832451772b4ab81 Mon Sep 17 00:00:00 2001 From: Noah Levenson Date: Thu, 20 Jul 2023 10:37:48 -0700 Subject: [PATCH 3/3] fix goroutine leak --- nats/discover.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/nats/discover.go b/nats/discover.go index bcbd8be..570aa56 100644 --- a/nats/discover.go +++ b/nats/discover.go @@ -150,6 +150,7 @@ func (nats *NATS) Discover() (*DiscoverResult, error) { var maddr stun.XORMappedAddress if err = maddr.GetFrom(trRes.Msg); err != nil { if err != nil { + <-filterDiscovDone return nil, fmt.Errorf("XOR-MAPPED-ADDRESS not found") } } @@ -167,6 +168,7 @@ func (nats *NATS) Discover() (*DiscoverResult, error) { var caddr attrAddress if err = caddr.getAs(trRes.Msg, attrTypeChangedAddress); err != nil { if err != nil { + <-filterDiscovDone return nil, fmt.Errorf("CHANGED-ADDRESS not found") } }