mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[PR #6185] [CLOSED] feat(inbounds): make port allocation atomic with an opt-in reservation table #6147
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#6147
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/6185
Author: @n0ctal
Created: 8/8/2026
Status: ❌ Closed
Base:
main← Head:upstream-port-reservations📝 Commits (5)
fc41979fix: serialize inbound port reservations0db23e3chore: apply goimports grouping to the port reservation filesd618273fix(inbounds): complete port reservation integration1e2c8b7fix(inbounds): lock ports during node snapshot and skip absent source tables38e3a64test(inbounds): cover the SQLite claim race through the real add path📊 Changes
17 files changed (+991 additions, -14 deletions)
View changed files
📝
README.md(+1 -0)📝
docs/content/docs/en/reference/env-vars.mdx(+1 -0)📝
docs/content/docs/fa/reference/env-vars.mdx(+1 -0)📝
docs/content/docs/ru/reference/env-vars.mdx(+1 -0)📝
docs/content/docs/zh/reference/env-vars.mdx(+1 -0)📝
internal/database/db.go(+1 -0)📝
internal/database/migrate_data.go(+10 -0)➕
internal/database/migrate_data_optional_table_test.go(+64 -0)📝
internal/database/model/model.go(+10 -0)📝
internal/web/service/inbound.go(+39 -12)📝
internal/web/service/inbound_node.go(+34 -0)➕
internal/web/service/inbound_node_portlock_test.go(+45 -0)📝
internal/web/service/port_conflict.go(+6 -2)➕
internal/web/service/port_reservation.go(+261 -0)➕
internal/web/service/port_reservation_test.go(+455 -0)📝
internal/web/service/server.go(+55 -0)📝
main.go(+6 -0)📄 Description
Summary
Add an opt-in port reservation table that makes inbound port allocation atomic, behind
XUI_ENFORCE_PORT_RESERVATIONS=1. Default behaviour is unchanged.This is a proposal rather than a drop-in fix — it introduces a table, so I would rather agree the shape with you than land it quietly. Happy to reshape or split it.
Why
checkPortConflictis a read, and the write that claims the port happens afterwards in a separate step. Two requests that create or update inbounds concurrently can both pass the check while neither has claimed anything yet, and both then take the same port.The panel ends up holding two inbounds on one port. Nothing in the panel objects — the conflict only surfaces when xray refuses to start, or when one listener wins and the other silently never accepts traffic. Reproducing it needs two overlapping requests, which is exactly what an API-driven or multi-operator setup produces, and it is invisible in a single-operator UI session.
Tightening the existing check cannot close this: any read-then-write pair has the same window. The claim has to be the same operation as the check.
Scope
inbound_port_reservationstable:(inbound_id, node_scope, listen, port, transport)with two unique indexes — one on the exact tuple, one on owner+transport — so a duplicate claim fails at the database rather than at review time.reserveInboundPortsTx/deleteInboundPortReservationsTxrun inside the same transaction as the inbound write.lockPortReservationKeysTx) serialise concurrent claims on the same key;0x5855is a fixed namespace withnode_scopeandportpacked into the remaining bits, so keys cannot collide.lockPortReservationKeys) covers the same key within one panel.AddInbound/UpdateInboundkeep callingcheckPortConflictunchanged when the gate is off.PrepareInboundPortReservations()at startup creates the table and backfills existing inbounds.Validation
go build ./internal/...clean.go test ./internal/web/service/green onmainatece16559with the gate off, which is the default.t.Setenvand cover concurrent claims, transport separation and node scoping.Risk
Off by default, so an install that does not set the variable behaves exactly as today.
One honest caveat: with the gate on, the table must exist, which means
PrepareInboundPortReservations()must have run. That holds for a normal start, but a test or embedding that constructs the service without going through startup will fail withno such table: inbound_port_reservationsrather than degrading. If you would prefer the gate to fall back tocheckPortConflictwhen the table is absent, that is a small change and I am glad to make it.The advisory-lock path is Postgres-only; SQLite relies on the unique indexes and the in-process mutex, which is sufficient for a single panel process but not for two processes on one SQLite file.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.