[PR #6225] [MERGED] fix(inbounds): close the port check-and-claim race on the serial writer #6186

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

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

  • fa1408f fix(inbounds): close the port check-and-claim race on the serial writer
  • 22454ec fix(inbounds): restore the port check UpdateInbound lost
  • 9c664ba chore: 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

AddInbound checks for a port conflict outside its transaction and then commits in a bare db.Transaction. Two overlapping creates both pass the read and both insert.

UpdateInbound and SetRemoteTraffic already run through runSerializedTx, so AddInbound is 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 in listenOverlaps, 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

  • checkPortConflict splits into a thin wrapper plus checkPortConflictTx(db, ...) that runs on a caller-supplied handle;
  • AddInbound moves from db.Transaction to runSerializedTx and evaluates the conflict inside;
  • UpdateInbound drops 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.Transaction takes the write lock at BEGIN (_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 AddInbound onto 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 to postCommitApply after commit, which is what the writer contract requires.

Validation

TestAddInboundConcurrentOverlappingListenersSingleWinner races a wildcard and a specific listener for the same port through AddInbound, 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 on main, reproduced.

go build ./..., go vet, golangci-lint run clean; go test ./internal/web/service green.

Risk

Moderate and worth stating plainly: AddInbound now 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 trade UpdateInbound already makes, and it removes the database is locked failures 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 inbounds on 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.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/6225 **Author:** [@n0ctal](https://github.com/n0ctal) **Created:** 8/16/2026 **Status:** ✅ Merged **Merged:** 8/18/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `upstream-serialize-inbound-create` --- ### 📝 Commits (3) - [`fa1408f`](https://github.com/MHSanaei/3x-ui/commit/fa1408fd2d5eba89e6acdff4d22db5715e1811e7) fix(inbounds): close the port check-and-claim race on the serial writer - [`22454ec`](https://github.com/MHSanaei/3x-ui/commit/22454eceeeee7904fc3e5a78abadfe83562294b5) fix(inbounds): restore the port check UpdateInbound lost - [`9c664ba`](https://github.com/MHSanaei/3x-ui/commit/9c664bad5a51a377a691b5a08d0b9c2cd8ae5247) chore: drop the accidentally committed dist build stub ### 📊 Changes **3 files changed** (+124 additions, -21 deletions) <details> <summary>View changed files</summary> 📝 `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) </details> ### 📄 Description ## Summary `AddInbound` checks for a port conflict outside its transaction and then commits in a bare `db.Transaction`. Two overlapping creates both pass the read and both insert. `UpdateInbound` and `SetRemoteTraffic` already run through `runSerializedTx`, so `AddInbound` is 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 in `listenOverlaps`, 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 - `checkPortConflict` splits into a thin wrapper plus `checkPortConflictTx(db, ...)` that runs on a caller-supplied handle; - `AddInbound` moves from `db.Transaction` to `runSerializedTx` and evaluates the conflict inside; - `UpdateInbound` drops 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.Transaction` takes the write lock at `BEGIN` (`_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 `AddInbound` onto 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 to `postCommitApply` after commit, which is what the writer contract requires. ## Validation `TestAddInboundConcurrentOverlappingListenersSingleWinner` races a wildcard and a specific listener for the same port through `AddInbound`, 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 on `main`, reproduced. `go build ./...`, `go vet`, `golangci-lint run` clean; `go test ./internal/web/service` green. ## Risk Moderate and worth stating plainly: `AddInbound` now 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 trade `UpdateInbound` already makes, and it removes the `database is locked` failures 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 `inbounds` on 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. --- <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:58 +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#6186
No description provided.