#6677·3x-ui

[Bug]: Hot-apply leaves on-disk bin/config.json stale until the next cold restart

Author: TheMRVXCreated Sep 29, 2026Updated Sep 29, 2026
Labelsbug

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 needed

However, 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:

  1. Future calls to RestartXray(false) evaluate process.GetConfig().Equals(xrayConfig) as true (configUnchanged == true), permanently skipping disk writes.
  2. The 30s cron ApplyPendingRestart() only checks isNeedXrayRestart, which is never set by updateSetting.
  3. If the server reboots, the systemd service restarts, or Xray crashes, Xray reads the stale bin/config.json from disk, silently wiping out all "saved" routing and template changes.
  4. Furthermore, because the manual Restart button was removed from the Routing and Outbounds tabs in commit 6b16d8c3 under the assumption that "Save now hot-applies", operators have no in-place way to force disk synchronization.

How to reproduce the problem

  1. Open the panel and navigate to Xray Settings -> Routing Rules (or Xray Configuration Template).
  2. Add a new routing rule (e.g., block destination UDP:443 or add a new domain routing rule).
  3. Click Save.
  4. Observe the panel toast: Settings saved successfully, and journal logs:
    INFO - Xray config changes applied through the core API, no restart needed.
  5. Check the actual file on disk via CLI:
    bash
    cat /usr/local/x-ui/bin/config.json
    Observation: The newly added rule is missing from bin/config.json on disk! The file timestamp reflects the previous cold start.
  6. Reboot the server or restart the systemd service:
    bash
    systemctl restart x-ui
  7. Re-check the running Xray configuration. The rule was wiped or never applied to the child process.

Expected behavior

  1. Whenever Xray settings or routing rules are saved, the configuration on disk (bin/config.json) must be immediately synchronized and persisted atomically.
  2. In-memory state and on-disk state must never diverge.
  3. 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): Calls a.XrayService.RestartXray(false) unconditionally.
  • internal/web/service/xray.go (RestartXray & tryHotApply): In RestartXray (lines 1371–1374):
    go
    if !isForce && !configUnchanged && s.tryHotApply(process, xrayConfig) {
        logger.Info("Xray config changes applied through the core API, no restart needed")
        return nil
    }
    In tryHotApply (lines 1515–1520):
    go
    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 true
    process.Start() is never reached. Writing to disk only exists in internal/xray/process.go:
    go
    writeFileAtomic(p.GetConfigPath(), p.config.GetJSON())
  • internal/web/job/xray.go (ApplyPendingRestart): Only checks isNeedXrayRestart, which is not set when modifying template settings.

Suggested Fixes

  1. Option A (Safest & Cleanest): Ensure updateSetting in xray_setting.go triggers a cold reload (RestartXray(true)). Routing tables, matcher trees, and DNS strategies are structural and require clean cold starts.
  2. Option B (Atomic Persistence on Hot-Apply): In tryHotApply(), before returning true, explicitly persist the config to disk:
    go
    if err := process.SaveConfig(); err != nil {
        return false // fallback to cold restart
    }
  3. 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