mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-07-16 01:26:07 +00:00
fix(xray): synchronize the process version and apiPort fields
Start writes p.version and p.apiPort (via refreshVersion/refreshAPIPort) after flipping the process to running, while GetXrayVersion and GetAPIPort read them lock-free from the status and traffic poll goroutines. The struct mutex deliberately excluded these fields, so a restart racing a poll was a real data race — a torn read of the version string header can crash. Extend the mutex to cover version and apiPort, doing the blocking version probe before taking the lock.
This commit is contained in:
@@ -54,3 +54,49 @@ func TestProcessLifecycleFieldsRaceSafe(t *testing.T) {
|
||||
close(stop)
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
// TestProcessVersionAPIPortRaceSafe writes version/apiPort the way Start's
|
||||
// refresh helpers do while GetXrayVersion/GetAPIPort read them concurrently.
|
||||
// Run with -race: an unsynchronized access to either field is reported.
|
||||
func TestProcessVersionAPIPortRaceSafe(t *testing.T) {
|
||||
inner := &process{
|
||||
logWriter: NewLogWriter(),
|
||||
config: &Config{InboundConfigs: []InboundConfig{{Tag: "api", Port: 12345}}},
|
||||
}
|
||||
p := &Process{inner}
|
||||
|
||||
var wg sync.WaitGroup
|
||||
stop := make(chan struct{})
|
||||
|
||||
wg.Go(func() {
|
||||
for {
|
||||
select {
|
||||
case <-stop:
|
||||
return
|
||||
default:
|
||||
}
|
||||
p.refreshAPIPort()
|
||||
inner.mu.Lock()
|
||||
inner.version = "v1.2.3"
|
||||
inner.mu.Unlock()
|
||||
}
|
||||
})
|
||||
|
||||
for range 4 {
|
||||
wg.Go(func() {
|
||||
for {
|
||||
select {
|
||||
case <-stop:
|
||||
return
|
||||
default:
|
||||
}
|
||||
_ = p.GetAPIPort()
|
||||
_ = p.GetXrayVersion()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
close(stop)
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user