mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-08-10 05:10:58 +00:00
fix(xray): synchronize lifecycle state (#6138)
* fix(xray): synchronize lifecycle snapshots Protect process replacement and result caching with a lifecycle state object, so read paths keep one process snapshot while restarts swap state safely. Bound version probing to prevent a stalled binary from holding the restart lock. * test(xray): cover concurrent lifecycle reads Exercise status, result, and traffic reads while the managed process is replaced, so the race detector guards the lifecycle snapshot boundary. * fix(xray): guard process config snapshots Synchronize hot-applied config snapshots, keep Telegram reads on one lifecycle snapshot, and strengthen lifecycle timeout and concurrency regression coverage. --------- Co-authored-by: PathGao <gaoyanbo@gaoyanbodeMacBook-Air.local>
This commit is contained in:
@@ -126,8 +126,9 @@ func NewTestProcess(xrayConfig *Config, configPath string) *Process {
|
||||
}
|
||||
|
||||
type process struct {
|
||||
// mu guards the process lifecycle fields (cmd, done, exitErr) plus version and
|
||||
// apiPort, which are written by Start/startCommand/refreshVersion/refreshAPIPort
|
||||
// mu guards the process lifecycle fields (cmd, done, exitErr) plus version,
|
||||
// apiPort, and config, which are written by Start/startCommand/refreshVersion/
|
||||
// refreshAPIPort/SetConfig
|
||||
// while being read concurrently by IsRunning/GetErr/GetResult/GetXrayVersion/
|
||||
// GetAPIPort/Stop from other goroutines (status endpoint, check-xray-running
|
||||
// and traffic jobs). Snapshot under the lock, then do any blocking syscall
|
||||
@@ -219,6 +220,7 @@ func (p *process) SetOnlineAPISupport(v OnlineAPISupport) {
|
||||
var (
|
||||
xrayGracefulStopTimeout = 5 * time.Second
|
||||
xrayForceStopTimeout = 2 * time.Second
|
||||
xrayVersionTimeout = 5 * time.Second
|
||||
// OnCrash is called when xray crashes unexpectedly. Set from web layer.
|
||||
OnCrash func(err error)
|
||||
)
|
||||
@@ -296,6 +298,8 @@ func (p *Process) GetAPIPort() int {
|
||||
|
||||
// GetConfig returns the configuration used by the Xray process.
|
||||
func (p *Process) GetConfig() *Config {
|
||||
p.mu.RLock()
|
||||
defer p.mu.RUnlock()
|
||||
return p.config
|
||||
}
|
||||
|
||||
@@ -303,6 +307,8 @@ func (p *Process) GetConfig() *Config {
|
||||
// process has been reconciled with it through the gRPC API (hot apply), so
|
||||
// later change detection compares against what is actually running.
|
||||
func (p *Process) SetConfig(config *Config) {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
p.config = config
|
||||
}
|
||||
|
||||
@@ -494,7 +500,9 @@ func (p *process) refreshAPIPort() {
|
||||
// refreshVersion updates the version string by running the Xray binary with -version.
|
||||
func (p *process) refreshVersion() {
|
||||
version := "Unknown"
|
||||
cmd := exec.CommandContext(context.Background(), GetBinaryPath(), "-version")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), xrayVersionTimeout)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, GetBinaryPath(), "-version")
|
||||
if data, err := cmd.Output(); err == nil {
|
||||
if datas := bytes.Split(data, []byte(" ")); len(datas) > 1 {
|
||||
version = string(datas[1])
|
||||
|
||||
Reference in New Issue
Block a user