mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-09-19 08:37:14 +00:00
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:
@@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user