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 <noreply@anthropic.com>
This commit is contained in:
Raja SubramanianandClaude Opus 4.8 authored and GitHub committed 2026-10-21 12:14:51 +05:30
1 parent 0a94c9cf9c
commit a8afbd69bd
1 file changed
+10 -1
@@ -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