wgengine/magicsock: fix data race from undocumented wireguard-go requirement

Endpoints need to be Stringers apparently.

Fixes tailscale/corp#422
This commit is contained in:
Brad Fitzpatrick
2020-07-03 22:26:53 -07:00
parent 9fbe8d7cf2
commit 5132edacf7
2 changed files with 27 additions and 0 deletions

View File

@@ -10,6 +10,7 @@ import (
"crypto/tls"
"encoding/binary"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
@@ -880,3 +881,22 @@ func TestDiscoMessage(t *testing.T) {
t.Error("failed to open it")
}
}
// tests that having a discoEndpoint.String prevents wireguard-go's
// log.Printf("%v") of its conn.Endpoint values from using reflect to
// walk into read mutex while they're being used and then causing data
// races.
func TestDiscoStringLogRace(t *testing.T) {
de := new(discoEndpoint)
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
fmt.Fprintf(ioutil.Discard, "%v", de)
}()
go func() {
defer wg.Done()
de.mu.Lock()
}()
wg.Wait()
}