[PR #5412] [MERGED] fix(traffic): prevent phantom quota consumption from stale node data #5874

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/5412
Author: @younesvatan78
Created: 6/17/2026
Status: ✅ Merged
Merged: 6/19/2026
Merged by: @MHSanaei

Base: main ← Head: main


📝 Commits (2)

  • dd14e27 fix(traffic): prevent phantom quota consumption from stale node data
  • 9e64231 Merge branch 'main' into main

📊 Changes

4 files changed (+122 additions, -43 deletions)

View changed files

📝 internal/web/service/client_traffic.go (+17 -10)
📝 internal/web/service/inbound_node.go (+2 -2)
📝 internal/web/service/inbound_traffic.go (+43 -15)
📝 internal/web/service/node_client_traffic_sum_test.go (+60 -16)

📄 Description

Summary

  • Root cause (phantom quota hit): `setRemoteTrafficLocked` seeded new `client_traffics` rows with `Up: cs.Up` — the node's full accumulated counter. When a node retained stale data from a previously-deleted account (e.g. deletion failed during an outage or PostgreSQL deadlock), that ghost counter was imported as real quota usage the moment the email was reused. `disableInvalidClients` ran in the same node-sync tick and immediately disabled the account.
  • PostgreSQL deadlock: `addClientTraffic` did a read-modify-write via `tx.Save(slice)`, issuing UPDATEs in slice order. Concurrent goroutines locking overlapping rows in opposite order deadlock on PostgreSQL (SQLite avoids this with file-level serialisation). Fixed with atomic per-email `UPDATE SET up=up+?` statements.
  • Stale `inbound_id` filters: `autoRenewClients` and `resetAllClientTrafficsLocked` filtered by `client_traffics.inbound_id`, which is set once on INSERT and never refreshed — unreliable after inbound deletes/recreates. Replaced with `client_inbounds` joins (authoritative source).

Changes

File Fix
`internal/web/service/inbound_node.go` Seed new rows at `Up: 0` / `Down: 0`; current node value becomes the baseline
`internal/web/service/inbound_traffic.go` Atomic per-email UPDATEs (no deadlock); persist delayed-start ExpiryTime conversion; email-based subquery in `autoRenewClients`; safe type assertion
`internal/web/service/client_traffic.go` `client_inbounds` join in `resetAllClientTrafficsLocked` instead of stale `inbound_id`
`internal/web/service/node_client_traffic_sum_test.go` Updated assertions + new `TestGhostData_NoPhantomTraffic` test reproducing the exact scenario

How the phantom happens (step by step)

  1. A client exists under one name (e.g. `user-old`) and is renamed to `user-new`.
  2. The rename triggers `DelClientStat("user-old")` + `AddClientStat("user-new")` in a single SQLite transaction on the master.
  3. A node (e.g. running PostgreSQL) had a previously-deleted `user-new` account whose deletion never propagated (node was offline or the delete transaction deadlocked). That ghost row still carries its old accumulated counter.
  4. The node sync job fires while the rename transaction is uncommitted — SQLite snapshot isolation means `user-new` is not yet visible in `existingEmails` → new-row path → `Up: cs.Up` imported as fresh usage.
  5. `AddClientStat("user-new")` commits with `OnConflict DoNothing` — the row already exists with the ghost counter, silent no-op.
  6. `disableInvalidClients` runs the same tick: used ≥ quota → account immediately disabled.

Fix: seed new rows at `Up: 0`, record the current node value as the baseline. Future delta = `cs.Up − baseline` = only real traffic beyond that point.

Test plan

  • `go test ./internal/web/service/...` passes (all 5 subpackages green)
  • `TestGhostData_NoPhantomTraffic` — new test explicitly covers the ghost import scenario
  • `TestUpgrade_PreExistingRow_NoDoubleCount` — pre-existing rows (upgrade path) still work correctly
  • Rename a client email while a node is temporarily offline; confirm counter stays at 0 after node reconnects
  • Verify `autoRenewClients` correctly renews clients on local inbounds only (not node inbounds)

🔄 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/5412 **Author:** [@younesvatan78](https://github.com/younesvatan78) **Created:** 6/17/2026 **Status:** ✅ Merged **Merged:** 6/19/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `main` --- ### 📝 Commits (2) - [`dd14e27`](https://github.com/MHSanaei/3x-ui/commit/dd14e27c088f00a7d7419276d3c02be47394efc4) fix(traffic): prevent phantom quota consumption from stale node data - [`9e64231`](https://github.com/MHSanaei/3x-ui/commit/9e64231262ef53e1ae7716b87607559ced69e61f) Merge branch 'main' into main ### 📊 Changes **4 files changed** (+122 additions, -43 deletions) <details> <summary>View changed files</summary> 📝 `internal/web/service/client_traffic.go` (+17 -10) 📝 `internal/web/service/inbound_node.go` (+2 -2) 📝 `internal/web/service/inbound_traffic.go` (+43 -15) 📝 `internal/web/service/node_client_traffic_sum_test.go` (+60 -16) </details> ### 📄 Description ## Summary - **Root cause (phantom quota hit):** \`setRemoteTrafficLocked\` seeded new \`client_traffics\` rows with \`Up: cs.Up\` — the node's full accumulated counter. When a node retained stale data from a previously-deleted account (e.g. deletion failed during an outage or PostgreSQL deadlock), that ghost counter was imported as real quota usage the moment the email was reused. \`disableInvalidClients\` ran in the same node-sync tick and immediately disabled the account. - **PostgreSQL deadlock:** \`addClientTraffic\` did a read-modify-write via \`tx.Save(slice)\`, issuing UPDATEs in slice order. Concurrent goroutines locking overlapping rows in opposite order deadlock on PostgreSQL (SQLite avoids this with file-level serialisation). Fixed with atomic per-email \`UPDATE SET up=up+?\` statements. - **Stale \`inbound_id\` filters:** \`autoRenewClients\` and \`resetAllClientTrafficsLocked\` filtered by \`client_traffics.inbound_id\`, which is set once on INSERT and never refreshed — unreliable after inbound deletes/recreates. Replaced with \`client_inbounds\` joins (authoritative source). ## Changes | File | Fix | |------|-----| | \`internal/web/service/inbound_node.go\` | Seed new rows at \`Up: 0\` / \`Down: 0\`; current node value becomes the baseline | | \`internal/web/service/inbound_traffic.go\` | Atomic per-email UPDATEs (no deadlock); persist delayed-start ExpiryTime conversion; email-based subquery in \`autoRenewClients\`; safe type assertion | | \`internal/web/service/client_traffic.go\` | \`client_inbounds\` join in \`resetAllClientTrafficsLocked\` instead of stale \`inbound_id\` | | \`internal/web/service/node_client_traffic_sum_test.go\` | Updated assertions + new \`TestGhostData_NoPhantomTraffic\` test reproducing the exact scenario | ## How the phantom happens (step by step) 1. A client exists under one name (e.g. \`user-old\`) and is **renamed** to \`user-new\`. 2. The rename triggers \`DelClientStat("user-old")\` + \`AddClientStat("user-new")\` in a single SQLite transaction on the master. 3. A node (e.g. running PostgreSQL) had a **previously-deleted** \`user-new\` account whose deletion never propagated (node was offline or the delete transaction deadlocked). That ghost row still carries its old accumulated counter. 4. The node sync job fires while the rename transaction is uncommitted — SQLite snapshot isolation means \`user-new\` is not yet visible in \`existingEmails\` → **new-row path** → \`Up: cs.Up\` imported as fresh usage. 5. \`AddClientStat("user-new")\` commits with \`OnConflict DoNothing\` — the row already exists with the ghost counter, silent no-op. 6. \`disableInvalidClients\` runs the same tick: used ≥ quota → account immediately disabled. **Fix:** seed new rows at \`Up: 0\`, record the current node value as the baseline. Future delta = \`cs.Up − baseline\` = only real traffic beyond that point. ## Test plan - [x] \`go test ./internal/web/service/...\` passes (all 5 subpackages green) - [x] \`TestGhostData_NoPhantomTraffic\` — new test explicitly covers the ghost import scenario - [x] \`TestUpgrade_PreExistingRow_NoDoubleCount\` — pre-existing rows (upgrade path) still work correctly - [x] Rename a client email while a node is temporarily offline; confirm counter stays at 0 after node reconnects - [x] Verify \`autoRenewClients\` correctly renews clients on local inbounds only (not node inbounds) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:04: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#5874
No description provided.