mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[PR #6225] [MERGED] fix(inbounds): close the port check-and-claim race on the serial writer #6186
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#6186
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/6225
Author: @n0ctal
Created: 8/16/2026
Status: ✅ Merged
Merged: 8/18/2026
Merged by: @MHSanaei
Base:
main← Head:upstream-serialize-inbound-create📝 Commits (3)
fa1408ffix(inbounds): close the port check-and-claim race on the serial writer22454ecfix(inbounds): restore the port check UpdateInbound lost9c664bachore: drop the accidentally committed dist build stub📊 Changes
3 files changed (+124 additions, -21 deletions)
View changed files
📝
internal/web/service/inbound.go(+17 -19)➕
internal/web/service/inbound_create_race_test.go(+99 -0)📝
internal/web/service/port_conflict.go(+8 -2)📄 Description
Summary
AddInboundchecks for a port conflict outside its transaction and then commits in a baredb.Transaction. Two overlapping creates both pass the read and both insert.UpdateInboundandSetRemoteTrafficalready run throughrunSerializedTx, soAddInboundis the one inbound writer left off the single traffic writer. Moving it there and folding the check into the transaction closes the window.Why
The check-and-claim gap is wide: tag resolution, client validation, email uniqueness and settings normalisation all sit between the read and the insert.
The case that no constraint can catch is a wildcard listener against a specific one on the same port —
("", 443, tcp)and("127.0.0.1", 443, tcp)are two different rows, so a unique index accepts both. The panel's real rule lives inlistenOverlaps, and only the semantic check can express it. That check is therefore load-bearing, and it is only sound if nothing can interleave between it and the insert.Scope
checkPortConflictsplits into a thin wrapper pluscheckPortConflictTx(db, ...)that runs on a caller-supplied handle;AddInboundmoves fromdb.TransactiontorunSerializedTxand evaluates the conflict inside;UpdateInbounddrops its pre-flight check and evaluates the same one inside the transaction it already has.No new table, no environment variable, no migration, no additional locking. On SQLite
db.Transactiontakes the write lock atBEGIN(_txlock=immediate), and on PostgreSQL the single writer goroutine provides the serialisation, so both backends are covered by the existing primitives.Two preconditions I checked before moving
AddInboundonto the writer: it is reached only from the two controller handlers, never from inside another traffic write, so the queue cannot re-enter; and the node push is already deferred topostCommitApplyafter commit, which is what the writer contract requires.Validation
TestAddInboundConcurrentOverlappingListenersSingleWinnerraces a wildcard and a specific listener for the same port throughAddInbound, 25 rounds, asserting exactly one commit.Mutation-checked against today's behaviour: restoring the pre-flight check and the bare transaction turns it red with
concurrent AddInbound committed=2, want exactly 1. That is the defect onmain, reproduced.go build ./...,go vet,golangci-lint runclean;go test ./internal/web/servicegreen.Risk
Moderate and worth stating plainly:
AddInboundnow waits for the traffic writer, so a create issued during a traffic poll is serialised behind it instead of contending with it. That is the same tradeUpdateInboundalready makes, and it removes thedatabase is lockedfailures a create could previously hit on SQLite after the busy timeout.This supersedes #6185, which solved the same race with an opt-in reservation table. That table is rebuilt from
inboundson every boot and import, so it is derived rather than authoritative, and its unique index cannot express the wildcard overlap rule — the in-transaction check was doing the real work there too. I will close it in favour of this.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.