[Bug]: Hot-apply leaves on-disk bin/config.json stale until the next cold restart
Describe the bug
When saving changes in Xray Settings / Routing / Outbounds, the panel invokes RestartXray(false) which attempts a runtime tryHotApply().
If tryHotApply() succeeds (or returns true via hotAPI.ApplyRoutingConfig()), the log records:
INFO - Xray config changes applied through the core API, no restart neededHowever, writeFileAtomic(GetConfigPath(), ...) is only ever called inside process.Start() (internal/xray/process.go). Because tryHotApply returns early with return nil, bin/config.json on disk is never rewritten.
Worse, tryHotApply calls process.SetConfig(newCfg) before returning, which marks the in-memory snapshot as "current". Consequently:
- Future calls to
RestartXray(false)evaluateprocess.GetConfig().Equals(xrayConfig)astrue(configUnchanged == true), permanently skipping disk writes. - The 30s cron
ApplyPendingRestart()only checksisNeedXrayRestart, which is never set byupdateSetting. - If the server reboots, the systemd service restarts, or Xray crashes, Xray reads the stale
bin/config.jsonfrom disk, silently wiping out all "saved" routing and template changes. - Furthermore, because the manual
Restartbutton was removed from the Routing and Outbounds tabs in commit6b16d8c3under the assumption that "Save now hot-applies", operators have no in-place way to force disk synchronization.
How to reproduce the problem
- Open the panel and navigate to Xray Settings -> Routing Rules (or Xray Configuration Template).
- Add a new routing rule (e.g., block destination
UDP:443or add a new domain routing rule). - Click Save.
- Observe the panel toast: Settings saved successfully, and journal logs:
INFO - Xray config changes applied through the core API, no restart needed. - Check the actual file on disk via CLI:Observation: The newly added rule is missing from
cat /usr/local/x-ui/bin/config.jsonbin/config.jsonon disk! The file timestamp reflects the previous cold start. - Reboot the server or restart the systemd service:
systemctl restart x-ui - Re-check the running Xray configuration. The rule was wiped or never applied to the child process.
Expected behavior
- Whenever Xray settings or routing rules are saved, the configuration on disk (
bin/config.json) must be immediately synchronized and persisted atomically. - In-memory state and on-disk state must never diverge.
- If hot-apply is retained, it must persist the new config to disk before returning, OR template/routing updates must trigger a clean process reload (
RestartXray(true)) to ensure complete re-indexing of routing tables and matcher trees.
Actual behavior
The panel marks the settings as saved, logs no restart needed, updates the database and in-memory struct, but leaves the disk configuration completely stale. A subsequent reboot or service restart silently reverts to the old configuration.
Root Cause Analysis (Code Inspection)
internal/web/controller/xray_setting.go(updateSetting): Callsa.XrayService.RestartXray(false)unconditionally.internal/web/service/xray.go(RestartXray&tryHotApply): InRestartXray(lines 1371–1374):Inif !isForce && !configUnchanged && s.tryHotApply(process, xrayConfig) { logger.Info("Xray config changes applied through the core API, no restart needed") return nil }tryHotApply(lines 1515–1520):if diff.RoutingConfig != nil { if err := hotAPI.ApplyRoutingConfig(diff.RoutingConfig); err != nil { logger.Info("hot apply: apply routing config failed:", err) return false } } process.SetConfig(newCfg) return trueprocess.Start()is never reached. Writing to disk only exists ininternal/xray/process.go:writeFileAtomic(p.GetConfigPath(), p.config.GetJSON())internal/web/job/xray.go(ApplyPendingRestart): Only checksisNeedXrayRestart, which is not set when modifying template settings.
Suggested Fixes
- Option A (Safest & Cleanest): Ensure
updateSettinginxray_setting.gotriggers a cold reload (RestartXray(true)). Routing tables, matcher trees, and DNS strategies are structural and require clean cold starts. - Option B (Atomic Persistence on Hot-Apply): In
tryHotApply(), before returningtrue, explicitly persist the config to disk:if err := process.SaveConfig(); err != nil { return false // fallback to cold restart } - UI Recommendation: Provide an explicit Save & Restart option or restore the Restart Xray action in the Routing/Outbounds view to allow operators to force cold reloads when necessary.
Environment & Versions
- 3x-ui version: 3.8.5 (v3.x / main branch)
- Xray-core version: 26.7.x+
- OS: Ubuntu 24.04 LTS (Linux x86_64)
- Install Method: install.sh script
Source: MHSanaei/3x-ui