[PR #6185] [CLOSED] feat(inbounds): make port allocation atomic with an opt-in reservation table #6147

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

📋 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)

  • fc41979 fix: serialize inbound port reservations
  • 0db23e3 chore: apply goimports grouping to the port reservation files
  • d618273 fix(inbounds): complete port reservation integration
  • 1e2c8b7 fix(inbounds): lock ports during node snapshot and skip absent source tables
  • 38e3a64 test(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

checkPortConflict is 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

  • New inbound_port_reservations table: (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 / deleteInboundPortReservationsTx run inside the same transaction as the inbound write.
  • Postgres advisory locks (lockPortReservationKeysTx) serialise concurrent claims on the same key; 0x5855 is a fixed namespace with node_scope and port packed into the remaining bits, so keys cannot collide.
  • An in-process mutex (lockPortReservationKeys) covers the same key within one panel.
  • AddInbound / UpdateInbound keep calling checkPortConflict unchanged 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 on main at ece16559 with the gate off, which is the default.
  • The feature's own tests set the gate per-case with t.Setenv and 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 with no such table: inbound_port_reservations rather than degrading. If you would prefer the gate to fall back to checkPortConflict when 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.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/6185 **Author:** [@n0ctal](https://github.com/n0ctal) **Created:** 8/8/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `upstream-port-reservations` --- ### 📝 Commits (5) - [`fc41979`](https://github.com/MHSanaei/3x-ui/commit/fc4197947e1b609682dbbbd8b5bb16a80420bfd1) fix: serialize inbound port reservations - [`0db23e3`](https://github.com/MHSanaei/3x-ui/commit/0db23e3fb3e9bde224bb04f1f73738f9cbec03b3) chore: apply goimports grouping to the port reservation files - [`d618273`](https://github.com/MHSanaei/3x-ui/commit/d618273ba622996796a89c95d8a32f30d329cfb2) fix(inbounds): complete port reservation integration - [`1e2c8b7`](https://github.com/MHSanaei/3x-ui/commit/1e2c8b7ab45fda125fc22d8200b0be6e525d33aa) fix(inbounds): lock ports during node snapshot and skip absent source tables - [`38e3a64`](https://github.com/MHSanaei/3x-ui/commit/38e3a64a933ef505c576a2a0b90a4f3413492451) test(inbounds): cover the SQLite claim race through the real add path ### 📊 Changes **17 files changed** (+991 additions, -14 deletions) <details> <summary>View changed files</summary> 📝 `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) </details> ### 📄 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 `checkPortConflict` is 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 - New `inbound_port_reservations` table: `(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` / `deleteInboundPortReservationsTx` run inside the same transaction as the inbound write. - Postgres advisory locks (`lockPortReservationKeysTx`) serialise concurrent claims on the same key; `0x5855` is a fixed namespace with `node_scope` and `port` packed into the remaining bits, so keys cannot collide. - An in-process mutex (`lockPortReservationKeys`) covers the same key within one panel. - `AddInbound` / `UpdateInbound` keep calling `checkPortConflict` unchanged 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 on `main` at `ece16559` with the gate off, which is the default. - The feature's own tests set the gate per-case with `t.Setenv` and 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 with `no such table: inbound_port_reservations` rather than degrading. If you would prefer the gate to fall back to `checkPortConflict` when 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:06:42 +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#6147
No description provided.