From a8afbd69bdffc89f86de413bb4f9a1ec1447ef8d Mon Sep 17 00:00:00 2001 From: Raja Subramanian Date: Wed, 21 Oct 2026 12:14:51 +0530 Subject: [PATCH] Fix data race on global logger in sfu downtrack integration test (#5036) TestDownTrackResumesAfterBandwidthPause called logger.InitFromConfig (SetLogger) mid-run while the hwstats CPU monitor goroutine, started by prometheus.Init in forwardstats_test, read the same non-atomic logger package globals. go test -race flagged the write-vs-read and failed CI. Set the global logger once in TestMain, before any test starts that goroutine, and drop the per-test InitFromConfig. Co-authored-by: Claude Opus 4.8 --- pkg/sfu/downtrack_downstream_integration_test.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pkg/sfu/downtrack_downstream_integration_test.go b/pkg/sfu/downtrack_downstream_integration_test.go index 99f1c3d0f..e16ecd765 100644 --- a/pkg/sfu/downtrack_downstream_integration_test.go +++ b/pkg/sfu/downtrack_downstream_integration_test.go @@ -45,6 +45,7 @@ import ( "encoding/binary" "io" "math" + "os" "sync" "sync/atomic" "testing" @@ -77,6 +78,15 @@ import ( "github.com/livekit/livekit-server/pkg/testutils/vnettest" ) +// TestMain sets the global logger once, before any test starts a background +// goroutine that reads it (prometheus.Init starts an hwstats CPU monitor that +// logs via the global logger). Calling logger.SetLogger from inside a test while +// that goroutine runs is a data race on the logger package globals. +func TestMain(m *testing.M) { + logger.InitFromConfig(&logger.Config{Level: "debug"}, "sfu-test") + os.Exit(m.Run()) +} + // ----------------------------------------------------------------------------- // packet capture on the far side // ----------------------------------------------------------------------------- @@ -1135,7 +1145,6 @@ func TestDownTrackResumesAfterBandwidthPause(t *testing.T) { } func testResumeAfterBandwidthPause(t *testing.T, allowPause bool) { - logger.InitFromConfig(&logger.Config{Level: "debug"}, t.Name()) const ( layer0Bps = 80_000 layer1Bps = 170_000