mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 16:04:50 +03:00
fix(database): guard inbound migrations against active PostgreSQL writes (#7321)
Prevent inbound data migration from waiting behind concurrent PostgreSQL node/client writes: acquire the complete write-table set with EXCLUSIVE NOWAIT before any DML, use ACCESS EXCLUSIVE only for tables requiring legacy DDL, and surface SQLSTATE 55P03 as retryable contention. Plain SELECT remains compatible with the normal lock set. Commit inbound normalization, orphan traffic cleanup and Vision flow repair together; propagate failures to CLI and restore callers and record transaction duration. Keep the reopened database pool private during host-settings restoration and inbound migration, with bounded retries of lock acquisition. Remove duplicate post-start migration and fail installation/update before service startup when migration fails. Preserve the stopped/disabled service unit so systemd and OpenRC updates can be retried; replace the unit only after successful migration. Verified reviewed head56e0372a09: all 12 CI jobs passed, including PostgreSQL contention/reader/private-pool regressions and Go/race. Detached test merge against maind6afe0ef3cwas conflict-free and git diff --check passed. Integrated local targeted tests passed for private pool, telemetry migration, SQLite rollback, and shared descendant selection (all eight subcases); PostgreSQL-specific tests skipped locally and verified in PR CI. Full integrated CI and live service-manager testing were not rerun. This is a scoped fix, not an atomic whole-database restore or proof of resolution of the historical 41-second SQL tail. Persistent external writers or post-import migration errors can still leave an already committed import requiring recovery; broader schema/restore maintenance coordination and writer occupancy investigation remain open. Review: https://github.com/MHSanaei/3x-ui/pull/7321#issuecomment-7364094359 Refs #7305 (keep open).
This commit is contained in:
+120
-3
@@ -31,7 +31,11 @@ func scriptFunction(t *testing.T, script, name string) string {
|
||||
const settingsCLIMock = `#!/bin/bash
|
||||
set -eu
|
||||
state=$TEST_SETTINGS_DIR
|
||||
if [[ "$1" == migrate ]]; then exit 0; fi
|
||||
if [[ "$1" == migrate ]]; then
|
||||
printf 'migrate\n' >> "$state/lifecycle"
|
||||
if [[ "$TEST_READ_MODE" == migration_failure ]]; then exit 1; fi
|
||||
exit 0
|
||||
fi
|
||||
shift
|
||||
case "$1" in
|
||||
-show)
|
||||
@@ -145,7 +149,8 @@ fail2ban_mode=install
|
||||
body := scriptFunction(t, script, parent)
|
||||
startCall := function
|
||||
if script == "update.sh" {
|
||||
startCall = "setup_fail2ban"
|
||||
startCall = `echo -e "${green}Migrating database...${plain}"`
|
||||
driver += scriptFunction(t, script, "_fail")
|
||||
}
|
||||
start := strings.Index(body, "\n "+startCall)
|
||||
if start < 0 {
|
||||
@@ -274,7 +279,7 @@ func TestScriptConfigReadFailureKeepsServiceSetup(t *testing.T) {
|
||||
if err != nil || !strings.Contains(out, "Unable to read webBasePath") {
|
||||
t.Errorf("service setup should continue after the settings warning: %v\n%s", err, out)
|
||||
}
|
||||
wantLifecycle, wantServices := "fail2ban\nunit\n", "daemon-reload\nenable x-ui\nstart x-ui\n"
|
||||
wantLifecycle, wantServices := "migrate\nfail2ban\nunit\n", "daemon-reload\nenable x-ui\nstart x-ui\n"
|
||||
if script == "install.sh" {
|
||||
wantServices = "daemon-reload\nenable x-ui\nrestart x-ui\n"
|
||||
}
|
||||
@@ -289,3 +294,115 @@ func TestScriptConfigReadFailureKeepsServiceSetup(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstallMigrationFailurePreventsServiceStart(t *testing.T) {
|
||||
dir, out, err := runScriptConfig(t, "install.sh", "/a/", false, "migration_failure", 1, true)
|
||||
if err == nil || !strings.Contains(out, "Database migration failed") {
|
||||
t.Fatalf("migration failure should abort the update: %v\n%s", err, out)
|
||||
}
|
||||
if got := readScriptState(t, dir, "restarts"); got != "" {
|
||||
t.Fatalf("service started after a failed migration: %q", got)
|
||||
}
|
||||
if got := readScriptState(t, dir, "lifecycle"); got != "migrate\n" {
|
||||
t.Fatalf("lifecycle after failed migration: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateMigrationFailureKeepsServiceUnitForRetry(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Skip("updater regression requires a POSIX environment")
|
||||
}
|
||||
bash, err := exec.LookPath("bash")
|
||||
if err != nil {
|
||||
t.Skip("bash is required for the isolated updater regression")
|
||||
}
|
||||
for _, distro := range []struct {
|
||||
name, unit, stopped, started string
|
||||
}{
|
||||
{"ubuntu", "systemd/x-ui.service", "systemctl stop x-ui\nsystemctl disable x-ui\n", "systemctl daemon-reload\nsystemctl enable x-ui\nsystemctl start x-ui\n"},
|
||||
{"alpine", "init.d/x-ui", "rc-service x-ui stop\nrc-update del x-ui\n", "rc-update add x-ui\nrc-service x-ui start\n"},
|
||||
} {
|
||||
t.Run(distro.name, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
for _, folder := range []string{"x-ui", "systemd", "init.d", "release/x-ui", "bin"} {
|
||||
if err := os.MkdirAll(filepath.Join(dir, folder), 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
const binary = `#!/bin/bash
|
||||
if [[ $1 == migrate ]]; then
|
||||
printf 'migrate\n' >> "$TEST_ROOT/events"
|
||||
[[ $TEST_FAIL_MIGRATION == 0 ]]
|
||||
else
|
||||
printf 'test version\n'
|
||||
fi
|
||||
`
|
||||
for name, value := range map[string]string{
|
||||
distro.unit: "old unit\n", "x-ui/x-ui": binary, "release/x-ui/x-ui": binary,
|
||||
"release/x-ui/x-ui.sh": "#!/bin/bash\n", "release/x-ui/x-ui.service": "new unit\n",
|
||||
} {
|
||||
if err := os.WriteFile(filepath.Join(dir, name), []byte(value), 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
driver := `red= green= yellow= blue= plain=
|
||||
xui_folder="$TEST_ROOT/x-ui"
|
||||
xui_service="$TEST_ROOT/systemd"
|
||||
release="$TEST_DISTRO"
|
||||
XUI_UPDATE_TAG=test
|
||||
curl_bin=fake_curl
|
||||
load_xui_env() { :; }
|
||||
setup_fail2ban() { :; }
|
||||
config_after_update() { :; }
|
||||
_xui_service_write_paths_dropin() { :; }
|
||||
_xui_warn_unsupported_hardening() { :; }
|
||||
chown() { :; }
|
||||
pkill() { :; }
|
||||
systemctl() { printf 'systemctl %s\n' "$*" >> "$TEST_ROOT/events"; }
|
||||
rc-service() { printf 'rc-service %s\n' "$*" >> "$TEST_ROOT/events"; }
|
||||
rc-update() { printf 'rc-update %s\n' "$*" >> "$TEST_ROOT/events"; }
|
||||
fake_curl() {
|
||||
case "$*" in
|
||||
*.sha256) printf 404 ;;
|
||||
*/x-ui.rc) printf 'new unit\n' > "$2" ;;
|
||||
*) tar -czf "$2" -C "$TEST_ROOT/release" x-ui ;;
|
||||
esac
|
||||
}
|
||||
`
|
||||
driver += scriptFunction(t, "update.sh", "_fail") + scriptFunction(t, "update.sh", "_install_xui_service_unit")
|
||||
driver += strings.NewReplacer(
|
||||
"/etc/init.d/x-ui", "$TEST_ROOT/init.d/x-ui",
|
||||
"/usr/bin/x-ui", "$TEST_ROOT/bin/x-ui",
|
||||
"/var/log/x-ui", "$TEST_ROOT/log",
|
||||
).Replace(scriptFunction(t, "update.sh", "update_x-ui"))
|
||||
driver += "update_x-ui\n"
|
||||
|
||||
run := func(failMigration string) (string, error) {
|
||||
t.Helper()
|
||||
cmd := exec.Command(bash, "-c", driver)
|
||||
cmd.Dir = dir
|
||||
cmd.Env = append(os.Environ(), "TEST_ROOT="+dir, "TEST_DISTRO="+distro.name, "TEST_FAIL_MIGRATION="+failMigration)
|
||||
out, err := cmd.CombinedOutput()
|
||||
return string(out), err
|
||||
}
|
||||
if out, err := run("1"); err == nil || !strings.Contains(out, "Database migration failed") {
|
||||
t.Fatalf("first update should fail in migration: %v\n%s", err, out)
|
||||
}
|
||||
if unit := readScriptState(t, dir, distro.unit); unit != "old unit\n" {
|
||||
t.Fatalf("failed update changed the service unit: %q", unit)
|
||||
}
|
||||
if got := readScriptState(t, dir, "events"); got != distro.stopped+"migrate\n" {
|
||||
t.Fatalf("failed update service actions: %q", got)
|
||||
}
|
||||
if out, err := run("0"); err != nil {
|
||||
t.Fatalf("ordinary update retry failed: %v\n%s", err, out)
|
||||
}
|
||||
if unit := readScriptState(t, dir, distro.unit); unit != "new unit\n" {
|
||||
t.Fatalf("retry did not install the replacement unit: %q", unit)
|
||||
}
|
||||
if got := readScriptState(t, dir, "events"); got != strings.Repeat(distro.stopped+"migrate\n", 2)+distro.started {
|
||||
t.Fatalf("retry service actions: %q", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user