wgengine/magicsock: fix latent data race in test

logBufWriter had no serialization.
It just so happens that none of its users currently ever log concurrently.
Make it safe for concurrent use.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
This commit is contained in:
Josh Bleecher Snyder 2021-07-08 16:16:09 -07:00 committed by Josh Bleecher Snyder
parent 01e159b610
commit 4f4dae32dd

View File

@ -1431,8 +1431,13 @@ func BenchmarkReceiveFrom_Native(b *testing.B) {
} }
} }
// logBufWriterMu serializes writes made by logBufWriter.
var logBufWriterMu sync.Mutex
func logBufWriter(buf *bytes.Buffer) logger.Logf { func logBufWriter(buf *bytes.Buffer) logger.Logf {
return func(format string, a ...interface{}) { return func(format string, a ...interface{}) {
logBufWriterMu.Lock()
defer logBufWriterMu.Unlock()
fmt.Fprintf(buf, format, a...) fmt.Fprintf(buf, format, a...)
if !bytes.HasSuffix(buf.Bytes(), []byte("\n")) { if !bytes.HasSuffix(buf.Bytes(), []byte("\n")) {
buf.WriteByte('\n') buf.WriteByte('\n')