[PR #7002] [MERGED] fix(update): keep Xray-core running until update downloads finish #6572

Closed
opened 2026-11-02 04:09:45 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/7002
Author: @eric15342335
Created: 10/28/2026
Status: ✅ Merged
Merged: 11/2/2026
Merged by: @MHSanaei

Base: main ← Head: claude/unruffled-sinoussi-682e7e


📝 Commits (2)

  • d6ed9f4 fix(xray): download and test new Xray-core before stopping the old one
  • bf08047 fix(update): use Panel Traffic Outbound for update.sh's GitHub downloads

📊 Changes

8 files changed (+421 additions, -96 deletions)

View changed files

📝 internal/web/service/panel/panel.go (+5 -0)
📝 internal/web/service/server.go (+47 -36)
➕ internal/web/service/server_xray_update_test.go (+207 -0)
📝 internal/web/service/xray.go (+5 -30)
➕ internal/xray/kernel_tun.go (+45 -0)
📝 internal/xray/process.go (+37 -0)
➕ internal/xray/process_check_test.go (+48 -0)
📝 update.sh (+27 -30)

📄 Description

Summary

On a server that can reach GitHub only through the Panel Traffic Outbound, updating Xray-core from the panel left Xray-core stopped and every client offline, and the panel update failed. With this PR, both updates download through that outbound, and neither stops Xray-core until its downloads are done.

Updating Xray-core (UpdateXray) now runs in this order:

  1. Download the release and verify its .dgst SHA-256, as before.
  2. Unpack the new binary next to the installed one.
  3. Run the new binary with run -test against the panel's current config.
  4. Rename the new binary over the installed one and restart Xray-core.

If step 1, 2 or 3 fails, the installed binary and the running Xray-core stay as they were. Windows still stops Xray-core just before step 4, because it can't replace a running executable.

Updating the panel (update.sh):

  1. The panel passes the outbound's loopback SOCKS proxy to update.sh as XUI_UPDATE_PROXY, and update.sh adds it to its GitHub curl calls.
  2. Nothing downloads after "Stopping x-ui". x-ui.sh now comes from the release archive, which ships it from the same commit as the binary, and Alpine's x-ui.rc downloads before the stop.

Notes:

  • XUI_UPDATE_PROXY uses socks5h, so GitHub's hostnames resolve through the outbound too. This server's own DNS may be filtered.
  • The server IP lookups at the end of update.sh stay direct, so the banner still shows this server's own address.
  • Step 3 runs WireGuard outbounds in userspace. Test mode builds every outbound, and one on a kernel TUN would add a second wg interface and IPv6 policy rules that the test run never removes. The panel's outbound probe already does the same for its temporary instance.

Why

Panel Traffic Outbound works through a loopback SOCKS inbound (panel-egress) that 3x-ui adds to Xray-core's config, so it only works while Xray-core runs.

  • UpdateXray stopped Xray-core before downloading. The download went direct and failed, and Xray-core stayed stopped. The stop counted as manual, so CheckXrayRunningJob didn't restart it.
  • The panel fetched update.sh through the outbound but never passed the proxy to the script, so the script's curl calls went direct and failed. With the proxy passed by hand, it still downloaded x-ui.sh (and x-ui.rc on Alpine) after "Stopping x-ui", which takes the proxy down too. The script then failed with the old install already removed, and the panel didn't come back.

The setting's description already says it carries "panel/Xray version checks and downloads", so this makes the updates match it.

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Documentation
  • Tests only
  • Build / CI / tooling
  • Other

Areas affected

  • Frontend (UI / panel pages)
  • Backend (API endpoints, login, settings)
  • Xray config generation
  • Subscription (share links / Clash / JSON)
  • Statistics / traffic counters
  • Database / migrations
  • Install / upgrade script
  • Docker image
  • Multi-node (sub-nodes)
  • Telegram bot

How was this tested?

  • New TestUpdateXray in internal/web/service/server_xray_update_test.go runs the real UpdateXray against shell-script fake cores and a local server standing in for GitHub:
    • A failed download leaves the running core alone. Before this change it stopped the core.
    • A core that fails run -test isn't installed. Before this change it was installed.
    • An accepted core replaces the running one.
    • When the restart is refused after the swap, the error says the new core is installed.
  • New TestCheckConfigKeepsWireGuardOffKernelTun in internal/xray/process_check_test.go checks that the config given to run -test has noKernelTun: true on WireGuard outbounds. It failed before the fix.
  • make verify passes on a clean clone of this branch, in Docker with Go 1.27.3, Node 26.12.1 and golangci-lint 2.15.0. The new tests also pass under -race, and shellcheck --severity=error and bash -n pass on update.sh.
  • End to end in a Debian 13 container with systemd and x-ui's real unit. All egress and DNS were blocked except a SOCKS proxy container, and Panel Traffic Outbound pointed at it:
    • Stock v3.10.2, updating Xray-core to v26.10.21: failed with write: operation not permitted, and Xray-core stayed down.
    • This branch, same update: succeeded in 4.7 s through the outbound. The old Xray-core kept serving until the swap, and no temp files were left.
    • This branch, with the download host blocked at the proxy: failed with EOF, and the running Xray-core was untouched (same PID, same binary).
    • This branch's update.sh with XUI_UPDATE_PROXY, on a fresh v3.10.2: upgraded to v3.11.0 with Xray-core 26.10.21. All 6 GitHub connections happened before "Stopping x-ui".
    • These ran on an earlier revision. The unit tests cover the review fixes made since then (the WireGuard handling in step 3 and the error wording).
  • Not tested: Alpine's x-ui.rc path (only shellchecked), the Windows swap (the Go test skips on Windows), and the panel's Update button together with the new script. The panel fetches update.sh from main, so I tested those two halves separately.

Breaking changes

None.

Checklist

  • I tested the change locally and confirmed the described behavior.
  • I added or updated tests for the new behavior (when applicable).
  • go build ./... and the test suite pass locally.
  • For frontend changes: npm run lint, npm run typecheck, and npm run build pass. (No frontend files changed. make verify ran them anyway.)
  • I updated the Wiki / README / API docs if user-facing behavior changed. (Not needed: the setting's description already promises this behavior.)
  • My commits follow the project's existing message style.
  • I have no unrelated changes mixed into this PR.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/7002 **Author:** [@eric15342335](https://github.com/eric15342335) **Created:** 10/28/2026 **Status:** ✅ Merged **Merged:** 11/2/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `claude/unruffled-sinoussi-682e7e` --- ### 📝 Commits (2) - [`d6ed9f4`](https://github.com/MHSanaei/3x-ui/commit/d6ed9f4e8038866b8683c407715395bb9e954409) fix(xray): download and test new Xray-core before stopping the old one - [`bf08047`](https://github.com/MHSanaei/3x-ui/commit/bf080477fc9ee4ca614ea9482ad3985751945a96) fix(update): use Panel Traffic Outbound for update.sh's GitHub downloads ### 📊 Changes **8 files changed** (+421 additions, -96 deletions) <details> <summary>View changed files</summary> 📝 `internal/web/service/panel/panel.go` (+5 -0) 📝 `internal/web/service/server.go` (+47 -36) ➕ `internal/web/service/server_xray_update_test.go` (+207 -0) 📝 `internal/web/service/xray.go` (+5 -30) ➕ `internal/xray/kernel_tun.go` (+45 -0) 📝 `internal/xray/process.go` (+37 -0) ➕ `internal/xray/process_check_test.go` (+48 -0) 📝 `update.sh` (+27 -30) </details> ### 📄 Description ## Summary On a server that can reach GitHub only through the Panel Traffic Outbound, updating Xray-core from the panel left Xray-core stopped and every client offline, and the panel update failed. With this PR, both updates download through that outbound, and neither stops Xray-core until its downloads are done. Updating Xray-core (`UpdateXray`) now runs in this order: 1. Download the release and verify its `.dgst` SHA-256, as before. 2. Unpack the new binary next to the installed one. 3. Run the new binary with `run -test` against the panel's current config. 4. Rename the new binary over the installed one and restart Xray-core. If step 1, 2 or 3 fails, the installed binary and the running Xray-core stay as they were. Windows still stops Xray-core just before step 4, because it can't replace a running executable. Updating the panel (`update.sh`): 1. The panel passes the outbound's loopback SOCKS proxy to `update.sh` as `XUI_UPDATE_PROXY`, and `update.sh` adds it to its GitHub curl calls. 2. Nothing downloads after "Stopping x-ui". `x-ui.sh` now comes from the release archive, which ships it from the same commit as the binary, and Alpine's `x-ui.rc` downloads before the stop. Notes: - `XUI_UPDATE_PROXY` uses `socks5h`, so GitHub's hostnames resolve through the outbound too. This server's own DNS may be filtered. - The server IP lookups at the end of `update.sh` stay direct, so the banner still shows this server's own address. - Step 3 runs WireGuard outbounds in userspace. Test mode builds every outbound, and one on a kernel TUN would add a second `wg` interface and IPv6 policy rules that the test run never removes. The panel's outbound probe already does the same for its temporary instance. ## Why Panel Traffic Outbound works through a loopback SOCKS inbound (`panel-egress`) that 3x-ui adds to Xray-core's config, so it only works while Xray-core runs. - `UpdateXray` stopped Xray-core before downloading. The download went direct and failed, and Xray-core stayed stopped. The stop counted as manual, so `CheckXrayRunningJob` didn't restart it. - The panel fetched `update.sh` through the outbound but never passed the proxy to the script, so the script's curl calls went direct and failed. With the proxy passed by hand, it still downloaded `x-ui.sh` (and `x-ui.rc` on Alpine) after "Stopping x-ui", which takes the proxy down too. The script then failed with the old install already removed, and the panel didn't come back. The setting's description already says it carries "panel/Xray version checks and downloads", so this makes the updates match it. ## Type of change - [x] Bug fix - [ ] New feature - [ ] Refactoring (no behavior change) - [ ] Documentation - [ ] Tests only - [ ] Build / CI / tooling - [ ] Other ## Areas affected - [ ] Frontend (UI / panel pages) - [x] Backend (API endpoints, login, settings) - [ ] Xray config generation - [ ] Subscription (share links / Clash / JSON) - [ ] Statistics / traffic counters - [ ] Database / migrations - [x] Install / upgrade script - [ ] Docker image - [ ] Multi-node (sub-nodes) - [ ] Telegram bot ## How was this tested? - New `TestUpdateXray` in `internal/web/service/server_xray_update_test.go` runs the real `UpdateXray` against shell-script fake cores and a local server standing in for GitHub: - A failed download leaves the running core alone. Before this change it stopped the core. - A core that fails `run -test` isn't installed. Before this change it was installed. - An accepted core replaces the running one. - When the restart is refused after the swap, the error says the new core is installed. - New `TestCheckConfigKeepsWireGuardOffKernelTun` in `internal/xray/process_check_test.go` checks that the config given to `run -test` has `noKernelTun: true` on WireGuard outbounds. It failed before the fix. - `make verify` passes on a clean clone of this branch, in Docker with Go 1.27.3, Node 26.12.1 and golangci-lint 2.15.0. The new tests also pass under `-race`, and `shellcheck --severity=error` and `bash -n` pass on `update.sh`. - End to end in a Debian 13 container with systemd and x-ui's real unit. All egress and DNS were blocked except a SOCKS proxy container, and Panel Traffic Outbound pointed at it: - Stock v3.10.2, updating Xray-core to v26.10.21: failed with `write: operation not permitted`, and Xray-core stayed down. - This branch, same update: succeeded in 4.7 s through the outbound. The old Xray-core kept serving until the swap, and no temp files were left. - This branch, with the download host blocked at the proxy: failed with `EOF`, and the running Xray-core was untouched (same PID, same binary). - This branch's `update.sh` with `XUI_UPDATE_PROXY`, on a fresh v3.10.2: upgraded to v3.11.0 with Xray-core 26.10.21. All 6 GitHub connections happened before "Stopping x-ui". - These ran on an earlier revision. The unit tests cover the review fixes made since then (the WireGuard handling in step 3 and the error wording). - Not tested: Alpine's `x-ui.rc` path (only shellchecked), the Windows swap (the Go test skips on Windows), and the panel's Update button together with the new script. The panel fetches `update.sh` from main, so I tested those two halves separately. ## Breaking changes None. ## Checklist - [x] I tested the change locally and confirmed the described behavior. - [x] I added or updated tests for the new behavior (when applicable). - [x] `go build ./...` and the test suite pass locally. - [x] For frontend changes: `npm run lint`, `npm run typecheck`, and `npm run build` pass. (No frontend files changed. `make verify` ran them anyway.) - [ ] I updated the Wiki / README / API docs if user-facing behavior changed. (Not needed: the setting's description already promises this behavior.) - [x] My commits follow the project's existing message style. - [x] I have no unrelated changes mixed into this PR. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi changed title from [PR #7002] fix(update): keep Xray-core running until update downloads finish to [PR #7002] [MERGED] fix(update): keep Xray-core running until update downloads finish 2026-11-07 14:34:02 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#6572
No description provided.