fix(logger): fix data race in InitLogger

Replace the package logger variable with an atomic.Pointer so InitLogger swapping the handle no longer races with concurrent Debug/Info/Warning/Error calls from other goroutines. Also guard fileRotate with a mutex, and add a regression test that reproduces the race under concurrent logging.
This commit is contained in:
Sanaei
2026-09-16 02:29:42 +02:00
parent ec9fbae645
commit 7ef22f94c9
2 changed files with 62 additions and 17 deletions
+30
View File
@@ -2,7 +2,10 @@ package logger
import (
"fmt"
"sync"
"testing"
golog "github.com/op/go-logging"
)
// TestGetLogs_ReturnsAtMostC guards the documented "up to c entries" contract.
@@ -28,3 +31,30 @@ func TestGetLogs_ReturnsAtMostC(t *testing.T) {
}
}
}
// InitLogger replaces the package logger while other goroutines are already
// logging — CI caught that as a data race between InitLogger and Warningf.
func TestInitLoggerConcurrentWithLogging(t *testing.T) {
t.Setenv("XUI_LOG_FOLDER", t.TempDir())
stop := make(chan struct{})
var logging sync.WaitGroup
logging.Add(1)
go func() {
defer logging.Done()
for {
select {
case <-stop:
return
default:
Warningf("concurrent %s", "log")
}
}
}()
for range 10 {
InitLogger(golog.CRITICAL)
}
close(stop)
logging.Wait()
}