fix: Xray restarting after being manually stopped (#2896) (#3329)
Some checks are pending
Release 3X-UI / build (386) (push) Waiting to run
Release 3X-UI / build (amd64) (push) Waiting to run
Release 3X-UI / build (arm64) (push) Waiting to run
Release 3X-UI / build (armv5) (push) Waiting to run
Release 3X-UI / build (armv6) (push) Waiting to run
Release 3X-UI / build (armv7) (push) Waiting to run
Release 3X-UI / build (s390x) (push) Waiting to run

This commit is contained in:
fgsfds 2025-08-07 23:35:11 +05:00 committed by GitHub
parent 4f25eb230e
commit ae08a29cde
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 33 additions and 10 deletions

View file

@ -169,9 +169,6 @@
<a-col> <a-col>
<a-icon type="bars" :style="{ cursor: 'pointer', float: 'right' }" @click="openLogs()"></a-icon> <a-icon type="bars" :style="{ cursor: 'pointer', float: 'right' }" @click="openLogs()"></a-icon>
</a-col> </a-col>
<a-col>
<a-icon type="bars" :style="{ cursor: 'pointer', float: 'right' }" @click="openXrayLogs()"></a-icon>
</a-col>
</a-row> </a-row>
</span> </span>
<template slot="content"> <template slot="content">

View file

@ -16,7 +16,7 @@ func NewCheckXrayRunningJob() *CheckXrayRunningJob {
} }
func (j *CheckXrayRunningJob) Run() { func (j *CheckXrayRunningJob) Run() {
if j.xrayService.IsXrayRunning() { if !j.xrayService.DidXrayCrash() {
j.checkTime = 0 j.checkTime = 0
} else { } else {
j.checkTime++ j.checkTime++

View file

@ -347,7 +347,6 @@ func (s *ServerService) StopXrayService() error {
} }
func (s *ServerService) RestartXrayService() error { func (s *ServerService) RestartXrayService() error {
s.xrayService.StopXray()
err := s.xrayService.RestartXray(true) err := s.xrayService.RestartXray(true)
if err != nil { if err != nil {
logger.Error("start xray failed:", err) logger.Error("start xray failed:", err)

View file

@ -3,6 +3,7 @@ package service
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"runtime"
"sync" "sync"
"x-ui/logger" "x-ui/logger"
@ -14,7 +15,8 @@ import (
var ( var (
p *xray.Process p *xray.Process
lock sync.Mutex lock sync.Mutex
isNeedXrayRestart atomic.Bool isNeedXrayRestart atomic.Bool // Indicates that restart was requested for Xray
isManuallyStopped atomic.Bool // Indicates that Xray was stopped manually from the panel
result string result string
) )
@ -32,7 +34,16 @@ func (s *XrayService) GetXrayErr() error {
if p == nil { if p == nil {
return nil return nil
} }
return p.GetErr()
err := p.GetErr()
if runtime.GOOS == "windows" && err.Error() == "exit status 1" {
// exit status 1 on Windows means that Xray process was killed
// as we kill process to stop in on Windows, this is not an error
return nil
}
return err
} }
func (s *XrayService) GetXrayResult() string { func (s *XrayService) GetXrayResult() string {
@ -45,7 +56,15 @@ func (s *XrayService) GetXrayResult() string {
if p == nil { if p == nil {
return "" return ""
} }
result = p.GetResult() result = p.GetResult()
if runtime.GOOS == "windows" && result == "exit status 1" {
// exit status 1 on Windows means that Xray process was killed
// as we kill process to stop in on Windows, this is not an error
return ""
}
return result return result
} }
@ -184,7 +203,8 @@ func (s *XrayService) GetXrayTraffic() ([]*xray.Traffic, []*xray.ClientTraffic,
func (s *XrayService) RestartXray(isForce bool) error { func (s *XrayService) RestartXray(isForce bool) error {
lock.Lock() lock.Lock()
defer lock.Unlock() defer lock.Unlock()
logger.Debug("restart xray, force:", isForce) logger.Debug("restart Xray, force:", isForce)
isManuallyStopped.Store(false)
xrayConfig, err := s.GetXrayConfig() xrayConfig, err := s.GetXrayConfig()
if err != nil { if err != nil {
@ -192,8 +212,8 @@ func (s *XrayService) RestartXray(isForce bool) error {
} }
if s.IsXrayRunning() { if s.IsXrayRunning() {
if !isForce && p.GetConfig().Equals(xrayConfig) { if !isForce && p.GetConfig().Equals(xrayConfig) && !isNeedXrayRestart.Load() {
logger.Debug("It does not need to restart xray") logger.Debug("It does not need to restart Xray")
return nil return nil
} }
p.Stop() p.Stop()
@ -205,12 +225,14 @@ func (s *XrayService) RestartXray(isForce bool) error {
if err != nil { if err != nil {
return err return err
} }
return nil return nil
} }
func (s *XrayService) StopXray() error { func (s *XrayService) StopXray() error {
lock.Lock() lock.Lock()
defer lock.Unlock() defer lock.Unlock()
isManuallyStopped.Store(true)
logger.Debug("Attempting to stop Xray...") logger.Debug("Attempting to stop Xray...")
if s.IsXrayRunning() { if s.IsXrayRunning() {
return p.Stop() return p.Stop()
@ -225,3 +247,8 @@ func (s *XrayService) SetToNeedRestart() {
func (s *XrayService) IsNeedRestartAndSetFalse() bool { func (s *XrayService) IsNeedRestartAndSetFalse() bool {
return isNeedXrayRestart.CompareAndSwap(true, false) return isNeedXrayRestart.CompareAndSwap(true, false)
} }
// Check if Xray is not running and wasn't stopped manually, i.e. crashed
func (s *XrayService) DidXrayCrash() bool {
return !s.IsXrayRunning() && !isManuallyStopped.Load()
}