[PR #6200] [MERGED] fix(traffic): apply maintenance side effects only after the commit lands #6165

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6200
Author: @n0ctal
Created: 8/10/2026
Status: ✅ Merged
Merged: 8/14/2026
Merged by: @MHSanaei

Base: main ← Head: upstream-traffic-after-commit


📝 Commits (3)

  • 7f33e4a fix(traffic): apply maintenance after durable commit
  • 1b331f1 fix(traffic): apply all runtime maintenance after commit
  • a4de417 Merge branch 'main' into upstream-traffic-after-commit

📊 Changes

11 files changed (+525 additions, -311 deletions)

View changed files

📝 frontend/src/generated/types.ts (+1 -0)
📝 frontend/src/generated/zod.ts (+3 -0)
📝 internal/web/service/global_traffic_test.go (+5 -5)
📝 internal/web/service/inbound.go (+0 -1)
📝 internal/web/service/inbound_autorenew_shadowsocks_test.go (+1 -1)
📝 internal/web/service/inbound_autorenew_test.go (+1 -1)
📝 internal/web/service/inbound_disable.go (+46 -97)
📝 internal/web/service/inbound_node.go (+22 -0)
📝 internal/web/service/inbound_traffic.go (+255 -206)
➕ internal/web/service/inbound_traffic_apply.go (+105 -0)
➕ internal/web/service/traffic_runtime_apply_test.go (+86 -0)

📄 Description

Summary

Move traffic-maintenance side effects out of the transaction bodies into a single post-commit batch, so a best-effort maintenance failure can no longer roll back usage that was already accepted.

This is the largest patch in this group. The size is deliberate: the point is the reordering, and a partial reordering would leave the two halves inconsistent.

Why

Traffic accounting and the maintenance that follows it — auto-renew, disabling depleted or expired clients, marking nodes dirty — run inside the same transaction, with runtime side effects issued from within it.

Two consequences:

  • A failure in best-effort maintenance rolls back the traffic deltas along with it. Usage that was correctly measured and accepted is discarded because a follow-up step failed.
  • Runtime side effects are issued before the transaction commits. If the commit then fails, the running core has already been told about a change the database never recorded.

Scope

  • Traffic deltas commit in their own transaction first.
  • Renew, disable and node-dirty marking commit in a second one.
  • Runtime side effects are collected into a batch and applied only after the commit, through the existing runtime abstraction, so local and node-backed inbounds follow the same path. A failed apply sets the restart flag as before.
  • An unrelated call to a helper from a not-yet-merged change was removed, so this patch stands alone.

Validation

  • go test ./... green; targeted -race for AddTraffic, AutoRenew, DisableInvalid and GlobalTraffic.

Not proven: crash injection between the first and second transaction was not performed against a real PostgreSQL. The semantics of that window are intentional — usage is durable and maintenance is retried on the next cycle — but the window itself was reasoned about rather than provoked.

Risk

Medium, because of size rather than mechanism. There is no schema or API change. Reviewing the two transaction boundaries and the post-commit batch is the whole of it; I am happy to split it further if you would rather see it in pieces.


🔄 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/6200 **Author:** [@n0ctal](https://github.com/n0ctal) **Created:** 8/10/2026 **Status:** ✅ Merged **Merged:** 8/14/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `upstream-traffic-after-commit` --- ### 📝 Commits (3) - [`7f33e4a`](https://github.com/MHSanaei/3x-ui/commit/7f33e4abd155d402cc2d4b0c9c441bfe031844b2) fix(traffic): apply maintenance after durable commit - [`1b331f1`](https://github.com/MHSanaei/3x-ui/commit/1b331f1ed94f63db6c773b088034d31294aba2e6) fix(traffic): apply all runtime maintenance after commit - [`a4de417`](https://github.com/MHSanaei/3x-ui/commit/a4de41769d3a865c8974c85ca6add28fbd5185e0) Merge branch 'main' into upstream-traffic-after-commit ### 📊 Changes **11 files changed** (+525 additions, -311 deletions) <details> <summary>View changed files</summary> 📝 `frontend/src/generated/types.ts` (+1 -0) 📝 `frontend/src/generated/zod.ts` (+3 -0) 📝 `internal/web/service/global_traffic_test.go` (+5 -5) 📝 `internal/web/service/inbound.go` (+0 -1) 📝 `internal/web/service/inbound_autorenew_shadowsocks_test.go` (+1 -1) 📝 `internal/web/service/inbound_autorenew_test.go` (+1 -1) 📝 `internal/web/service/inbound_disable.go` (+46 -97) 📝 `internal/web/service/inbound_node.go` (+22 -0) 📝 `internal/web/service/inbound_traffic.go` (+255 -206) ➕ `internal/web/service/inbound_traffic_apply.go` (+105 -0) ➕ `internal/web/service/traffic_runtime_apply_test.go` (+86 -0) </details> ### 📄 Description ## Summary Move traffic-maintenance side effects out of the transaction bodies into a single post-commit batch, so a best-effort maintenance failure can no longer roll back usage that was already accepted. This is the largest patch in this group. The size is deliberate: the point is the reordering, and a partial reordering would leave the two halves inconsistent. ## Why Traffic accounting and the maintenance that follows it — auto-renew, disabling depleted or expired clients, marking nodes dirty — run inside the same transaction, with runtime side effects issued from within it. Two consequences: - A failure in best-effort maintenance rolls back the traffic deltas along with it. Usage that was correctly measured and accepted is discarded because a follow-up step failed. - Runtime side effects are issued before the transaction commits. If the commit then fails, the running core has already been told about a change the database never recorded. ## Scope - Traffic deltas commit in their own transaction first. - Renew, disable and node-dirty marking commit in a second one. - Runtime side effects are collected into a batch and applied only after the commit, through the existing runtime abstraction, so local and node-backed inbounds follow the same path. A failed apply sets the restart flag as before. - An unrelated call to a helper from a not-yet-merged change was removed, so this patch stands alone. ## Validation - `go test ./...` green; targeted `-race` for `AddTraffic`, `AutoRenew`, `DisableInvalid` and `GlobalTraffic`. Not proven: crash injection between the first and second transaction was not performed against a real PostgreSQL. The semantics of that window are intentional — usage is durable and maintenance is retried on the next cycle — but the window itself was reasoned about rather than provoked. ## Risk Medium, because of size rather than mechanism. There is no schema or API change. Reviewing the two transaction boundaries and the post-commit batch is the whole of it; I am happy to split it further if you would rather see it in pieces. --- <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:49 +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#6165
No description provided.