From 4f4dae32ddc003ba5272a4a7f1ecb1735ac73581 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Thu, 8 Jul 2021 16:16:09 -0700 Subject: [PATCH] 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 --- wgengine/magicsock/magicsock_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/wgengine/magicsock/magicsock_test.go b/wgengine/magicsock/magicsock_test.go index c116bcc40..cbe14fbc3 100644 --- a/wgengine/magicsock/magicsock_test.go +++ b/wgengine/magicsock/magicsock_test.go @@ -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 { return func(format string, a ...interface{}) { + logBufWriterMu.Lock() + defer logBufWriterMu.Unlock() fmt.Fprintf(buf, format, a...) if !bytes.HasSuffix(buf.Bytes(), []byte("\n")) { buf.WriteByte('\n')