mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[PR #6729] [MERGED] fix(update): run the database migration before starting the service #6432
Labels
No labels
backlog
bug
clarification needed
documentation
duplicate
enhancement
github_actions
good first issue
invalid
javascript
New Feature
PR welcomed
pull-request
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
MHSanaei/3x-ui#6432
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/MHSanaei/3x-ui/pull/6729
Author: @mrnickson-hue
Created: 10/4/2026
Status: ✅ Merged
Merged: 10/5/2026
Merged by: @MHSanaei
Base:
main← Head:fix/6728-migration-race📝 Commits (2)
2ccf1f6fix(update): run the database migration before starting the serviced5d0883Merge branch 'main' into fix/6728-migration-race📊 Changes
1 file changed (+5 additions, -0 deletions)
View changed files
📝
update.sh(+5 -0)📄 Description
Summary
update.shnow runsx-ui migrateto completion before starting the service. The freshly started service andconfig_after_update's CLI calls therefore no longer run the schema migrations on the same database at the same time, which is the race reported in #6728.Why
Closes #6728.
update_x-ui()ransystemctl start x-uiand then immediatelyconfig_after_update, which callsx-ui setting -show trueandx-ui migrate. All three processes callInitDB(), and with it every migration, concurrently. On an upgrade that adds schema, the loser exits with an error. 3.8.5 → 3.9.0 failed withduplicate column name: exclude_from_sub, and the panel only came back because the unit hasRestart=on-failure.The fix suggested in the issue triage is not sufficient. I implemented it first: routing the three column helpers'
AddColumnerror throughisIgnorableDuplicateColumnErr, with a red→green unit test. Then I ran it against a real 3.8.5 database. With the column race tolerated, the two processes get further and collide on the tables that are new in 3.9.0:Failures went up, from 11/30 to 20/30 (table below). Concurrent
InitDB()is unsafe in general (columns, tables, the inbounds rebuild), so tolerating one error class only moves the race. This PR removes the concurrency on the update path instead, and the Go change is not included.This also brings
update.shin line withinstall.sh, whereconfig_after_installalready runsx-ui migratebefore the firstsystemctl start.Type of change
Areas affected
How was this tested?
No unit test can reach a shell script's step ordering (per CLAUDE.md, the command that demonstrates it is named instead). I reproduced the update sequence against a real 3.8.5 production database copy, taken with
sqlite3 .backup, with binaries built from this branch's base (815c9c57):XUI_DB_FOLDERandXUI_LOG_FOLDERpoint to a temp dir;x-ui setting -show trueprocesses start at once, the same overlapupdate.shcreates;Database initialization failed,Error initializing databaseorError auto migrating.integrity_checkupdate.shorder: two concurrentInitDB(control)x-ui migratefirst, then the two concurrent callsC and D are identical, so the Go change adds nothing once the update path is serialised.
The core of each run:
Also checked:
bash -n update.shpasses.shellcheck update.shreports 129 findings before and after, so none is new. The new path is quoted to avoid adding an SC2086.alpine/ systemd branch, so both the OpenRC and the systemd start are covered.${xui_folder}/x-uiis already installed at that point, since the line just above chmods itsbin/config.json.x-ui migrateisInitDB()+InboundService.MigrateDB(). Both are idempotent and touch only the database, not Xray, so running them while the service is stopped is safe.config_after_updatestill runs it again afterwards as a no-op.update.shfrommain(panelUpdaterURL), so after merge the fix also applies to panels updating from older versions.main. On the branch with the Go experiment:make verifygen-check,golangci-lint(0 issues),lint,format:checkandtypecheckpassed;go test ./...(48 packages) andgo test -race ./internal/database/...passed;make build build-storybookpassed.Two things I hit locally that are pre-existing on
mainand unrelated to this PR:make msw-worker-checkfails becausefrontend/public/mockServiceWorker.jsis still the 2.15.0 worker while99bc68fabumpedmswto 3.0.1. CI does not run this target.client-bulk-calendar-renewal,client-calendar-renewal,inbound-form-modal). They time out identically on cleanmain.Screenshots / recordings
N/A, no UI change.
Breaking changes
None. Updates print one extra step,
Migrating database..., followed by the existingx-ui migrateoutput.Checklist
go build ./...and the test suite pass locally.npm run lint,npm run typecheck, andnpm run buildpass. No frontend changes.Open question for the maintainer. This PR closes the update path, but any other overlap of two
InitDB()callers is still unsafe, for example running anx-ui settingcommand while the service is (re)starting. A complete fix would be a cross-process migration lock inInitDB(): an advisory file lock next to the SQLite DB andpg_advisory_lockon PostgreSQL. That is a larger, platform-specific change, so I did not include it unasked. Happy to send it as a follow-up if you want it.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.