[GH-ISSUE #5490] [Bug]: Incorrect traffic aggregation with PostgreSQL + Multi-Node: client_traffics usage much higher than node_client_traffics sum #3836

Closed
opened 2026-10-31 20:35:18 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @ali-hosini on GitHub (Jun 22, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/5490

Describe the bug

Incorrect Traffic Aggregation in Multi-Node Setup (PostgreSQL)

Description

I am running 3x-ui v3.3.1 with PostgreSQL and multiple connected nodes.

Some users have reported that their traffic usage suddenly jumps far above their actual usage and their accounts become disabled because the quota is reached.

After investigating the database, I found a major inconsistency between client_traffics and node_client_traffics.


Environment

  • 3x-ui Version: v3.3.1
  • Database: PostgreSQL
  • Deployment Type: Multi-Node
  • Number of Nodes: 8

Example User

u104848997o9

Data Stored in client_traffics

Query:

SELECT email, up, down, up + down AS used, total, enable
FROM client_traffics
WHERE email = 'u104848997o9';

Result:

email        | up         | down        | used        | total       | enable
-------------+------------+-------------+-------------+-------------+--------
u104848997o9 | 5385399238 | 16228887297 | 21614286535 | 21474836480 | false

According to client_traffics, the user consumed:

21,614,286,535 bytes ≈ 20.13 GiB

The user's quota is:

21,474,836,480 bytes = 20 GiB

Therefore the account was automatically disabled.


Data Stored in node_client_traffics

Query:

SELECT
    node_id,
    SUM(up) AS total_up,
    SUM(down) AS total_down,
    SUM(up + down) AS total_used
FROM node_client_traffics
WHERE email = 'u104848997o9'
GROUP BY node_id
ORDER BY node_id;

Result:

node_id | total_up  | total_down | total_used
--------+-----------+------------+------------
1       | 277492    | 1128583    | 1406075
2       | 85883029  | 1005016621 | 1090899650
4       | 68119     | 804525     | 872644
6       | 10169     | 12663      | 22832
7       | 117276710 | 342777445  | 460054155

Total traffic reported by all nodes:

Up:    203,515,519 bytes
Down:  1,349,739,837 bytes
Total: 1,553,255,356 bytes

Which equals:

≈ 1.45 GiB

The Problem

The panel reports:

client_traffics = 20.13 GiB

But the sum of all node traffic is:

node_client_traffics = 1.45 GiB

Difference:

≈ 18.68 GiB

As a result, the user account is disabled even though the real traffic usage is still far below the assigned quota.


Database Information

Table structure:

\d node_client_traffics

Relevant index:

"idx_node_email" UNIQUE, btree (node_id, email)

This indicates that node_client_traffics stores a single aggregated row per (node_id, email) pair rather than historical records.

Current row counts:

SELECT COUNT(*) FROM client_traffics;

Result:

901
SELECT COUNT(*) FROM node_client_traffics;

Result:

4659

Expected Behavior

The value stored in client_traffics should match the aggregated traffic reported by all related rows in node_client_traffics.

Users should only be disabled when their actual traffic usage reaches the configured quota.


Actual Behavior

The value stored in client_traffics becomes significantly larger than the traffic reported by nodes.

Example:

client_traffics:      ~20.13 GiB
node_client_traffics: ~1.45 GiB

This causes false quota exhaustion and incorrect account suspension.


Possible Cause

Based on the observed data, there may be a bug in the traffic synchronization or aggregation logic when using PostgreSQL with multiple nodes.

Potential causes:

  • Traffic deltas being added multiple times
  • Previously synchronized values being re-applied
  • Incorrect aggregation logic in multi-node mode
  • Race condition during node synchronization
  • client_traffics being incremented repeatedly instead of reflecting actual node totals

Impact

This issue directly affects quota-based users.

Accounts may become disabled even though their actual traffic usage is significantly below their configured quota.


Additional Notes

I can provide additional database samples and logs if needed.

The issue has already been observed on production users and appears reproducible in a PostgreSQL multi-node environment.

How to reproduce the problem

  1. Deploy a fresh 3x-ui v3.3.1 installation using PostgreSQL.

  2. Configure multiple nodes (8 nodes in my case) and connect them to the main panel.

  3. Create a quota-based user (for example, a 20 GB traffic limit).

  4. Use the account through one or more nodes and generate traffic normally.

  5. After some time, compare the values stored in:

    • client_traffics
    • node_client_traffics
  6. In some cases, client_traffics.up + client_traffics.down becomes significantly larger than the sum of all related rows in node_client_traffics.

  7. Once the inflated value exceeds the configured quota, the account is automatically disabled even though the actual traffic reported by nodes is much lower.

Note: I have not yet identified the exact trigger condition. However, the issue has been observed multiple times in a PostgreSQL multi-node environment and the database values clearly show a mismatch between client_traffics and the aggregated node traffic.

I have already confirmed that the affected user's traffic in node_client_traffics is approximately 1.45 GiB, while client_traffics shows approximately 20.13 GiB. Therefore, this does not appear to be actual user traffic consumption.

Expected behavior

Additional Findings

After investigating further, I wrote a verification script that compares:

client_traffics.up + client_traffics.down

against:

SUM(node_client_traffics.up + node_client_traffics.down)

for every user.

Results:

Total users checked: 901
Affected users found: 70

Examples:

u104848997o9
client: 20.13 GB
nodes : 1.45 GB
diff  : 18.68 GB

zwlfqshq94
client: 116.21 GB
nodes : 23.06 GB
diff  : 93.15 GB

86yntqqoko
client: 57.83 GB
nodes : 17.36 GB
diff  : 40.47 GB

j7deico546
client: 50.01 GB
nodes : 9.39 GB
diff  : 40.61 GB

u105751777o26
client: 51.31 GB
nodes : 15.54 GB
diff  : 35.77 GB

Many affected users were automatically disabled because the inflated value in client_traffics exceeded their quota, even though the aggregated traffic reported by nodes was significantly lower.

This confirms that the issue is not limited to a single account and appears to affect multiple users in the same PostgreSQL multi-node environment.

Actual behavior

No response

Relevant logs


Screenshots

Image

3x-ui version

3.3.1

Xray-core version

26.6.1

How did you install 3x-ui?

install.sh script

Operating system

ubuntu 2022

Which parts of the panel are affected?

No response

Browser (only if it is a UI bug)

No response

Is the panel behind a reverse proxy or CDN?

None

Before submitting

  • I searched existing issues and this bug has not been reported.
  • I am running the latest released version of 3x-ui (or have verified the bug still exists on it).
  • This bug report is written in English.
  • I have redacted any sensitive data (tokens, real domains, client UUIDs).
Originally created by @ali-hosini on GitHub (Jun 22, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/5490 ### Describe the bug # Incorrect Traffic Aggregation in Multi-Node Setup (PostgreSQL) ## Description I am running **3x-ui v3.3.1** with **PostgreSQL** and multiple connected nodes. Some users have reported that their traffic usage suddenly jumps far above their actual usage and their accounts become disabled because the quota is reached. After investigating the database, I found a major inconsistency between `client_traffics` and `node_client_traffics`. --- ## Environment * 3x-ui Version: `v3.3.1` * Database: `PostgreSQL` * Deployment Type: `Multi-Node` * Number of Nodes: `8` --- ## Example User ```text u104848997o9 ``` --- ## Data Stored in client_traffics Query: ```sql SELECT email, up, down, up + down AS used, total, enable FROM client_traffics WHERE email = 'u104848997o9'; ``` Result: ```text email | up | down | used | total | enable -------------+------------+-------------+-------------+-------------+-------- u104848997o9 | 5385399238 | 16228887297 | 21614286535 | 21474836480 | false ``` According to `client_traffics`, the user consumed: ```text 21,614,286,535 bytes ≈ 20.13 GiB ``` The user's quota is: ```text 21,474,836,480 bytes = 20 GiB ``` Therefore the account was automatically disabled. --- ## Data Stored in node_client_traffics Query: ```sql SELECT node_id, SUM(up) AS total_up, SUM(down) AS total_down, SUM(up + down) AS total_used FROM node_client_traffics WHERE email = 'u104848997o9' GROUP BY node_id ORDER BY node_id; ``` Result: ```text node_id | total_up | total_down | total_used --------+-----------+------------+------------ 1 | 277492 | 1128583 | 1406075 2 | 85883029 | 1005016621 | 1090899650 4 | 68119 | 804525 | 872644 6 | 10169 | 12663 | 22832 7 | 117276710 | 342777445 | 460054155 ``` Total traffic reported by all nodes: ```text Up: 203,515,519 bytes Down: 1,349,739,837 bytes Total: 1,553,255,356 bytes ``` Which equals: ```text ≈ 1.45 GiB ``` --- ## The Problem The panel reports: ```text client_traffics = 20.13 GiB ``` But the sum of all node traffic is: ```text node_client_traffics = 1.45 GiB ``` Difference: ```text ≈ 18.68 GiB ``` As a result, the user account is disabled even though the real traffic usage is still far below the assigned quota. --- ## Database Information Table structure: ```sql \d node_client_traffics ``` Relevant index: ```text "idx_node_email" UNIQUE, btree (node_id, email) ``` This indicates that `node_client_traffics` stores a single aggregated row per `(node_id, email)` pair rather than historical records. Current row counts: ```sql SELECT COUNT(*) FROM client_traffics; ``` Result: ```text 901 ``` ```sql SELECT COUNT(*) FROM node_client_traffics; ``` Result: ```text 4659 ``` --- ## Expected Behavior The value stored in `client_traffics` should match the aggregated traffic reported by all related rows in `node_client_traffics`. Users should only be disabled when their actual traffic usage reaches the configured quota. --- ## Actual Behavior The value stored in `client_traffics` becomes significantly larger than the traffic reported by nodes. Example: ```text client_traffics: ~20.13 GiB node_client_traffics: ~1.45 GiB ``` This causes false quota exhaustion and incorrect account suspension. --- ## Possible Cause Based on the observed data, there may be a bug in the traffic synchronization or aggregation logic when using PostgreSQL with multiple nodes. Potential causes: * Traffic deltas being added multiple times * Previously synchronized values being re-applied * Incorrect aggregation logic in multi-node mode * Race condition during node synchronization * `client_traffics` being incremented repeatedly instead of reflecting actual node totals --- ## Impact This issue directly affects quota-based users. Accounts may become disabled even though their actual traffic usage is significantly below their configured quota. --- ## Additional Notes I can provide additional database samples and logs if needed. The issue has already been observed on production users and appears reproducible in a PostgreSQL multi-node environment. ### How to reproduce the problem 1. Deploy a fresh 3x-ui v3.3.1 installation using PostgreSQL. 2. Configure multiple nodes (8 nodes in my case) and connect them to the main panel. 3. Create a quota-based user (for example, a 20 GB traffic limit). 4. Use the account through one or more nodes and generate traffic normally. 5. After some time, compare the values stored in: * `client_traffics` * `node_client_traffics` 6. In some cases, `client_traffics.up + client_traffics.down` becomes significantly larger than the sum of all related rows in `node_client_traffics`. 7. Once the inflated value exceeds the configured quota, the account is automatically disabled even though the actual traffic reported by nodes is much lower. Note: I have not yet identified the exact trigger condition. However, the issue has been observed multiple times in a PostgreSQL multi-node environment and the database values clearly show a mismatch between `client_traffics` and the aggregated node traffic. I have already confirmed that the affected user's traffic in node_client_traffics is approximately 1.45 GiB, while client_traffics shows approximately 20.13 GiB. Therefore, this does not appear to be actual user traffic consumption. ### Expected behavior ## Additional Findings After investigating further, I wrote a verification script that compares: ```sql client_traffics.up + client_traffics.down ``` against: ```sql SUM(node_client_traffics.up + node_client_traffics.down) ``` for every user. Results: ```text Total users checked: 901 Affected users found: 70 ``` Examples: ```text u104848997o9 client: 20.13 GB nodes : 1.45 GB diff : 18.68 GB zwlfqshq94 client: 116.21 GB nodes : 23.06 GB diff : 93.15 GB 86yntqqoko client: 57.83 GB nodes : 17.36 GB diff : 40.47 GB j7deico546 client: 50.01 GB nodes : 9.39 GB diff : 40.61 GB u105751777o26 client: 51.31 GB nodes : 15.54 GB diff : 35.77 GB ``` Many affected users were automatically disabled because the inflated value in `client_traffics` exceeded their quota, even though the aggregated traffic reported by nodes was significantly lower. This confirms that the issue is not limited to a single account and appears to affect multiple users in the same PostgreSQL multi-node environment. ### Actual behavior _No response_ ### Relevant logs ```shell ``` ### Screenshots <img width="924" height="929" alt="Image" src="https://github.com/user-attachments/assets/53e24ac6-3197-4e2a-bff9-ff800eda71f8" /> ### 3x-ui version 3.3.1 ### Xray-core version 26.6.1 ### How did you install 3x-ui? install.sh script ### Operating system ubuntu 2022 ### Which parts of the panel are affected? _No response_ ### Browser (only if it is a UI bug) _No response_ ### Is the panel behind a reverse proxy or CDN? None ### Before submitting - [x] I searched [existing issues](../issues?q=is%3Aissue) and this bug has not been reported. - [x] I am running the latest released version of 3x-ui (or have verified the bug still exists on it). - [x] This bug report is written in English. - [x] I have redacted any sensitive data (tokens, real domains, client UUIDs).
bsakaguchi 2026-10-31 20:35:18 +07:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@github-actions[bot] commented on GitHub (Jun 22, 2026):

The root cause is confirmed and has been fixed in the main branch (commit 679d2e1, PR #5477, merged 2026-06-21). The fix is not yet in a tagged release; v3.3.1 (released 2026-06-12) still carries the bug.

What went wrong

In internal/web/service/inbound_node.go, the per-node delta is computed as:

delta = node_current_counter - stored_baseline (node_client_traffics)

When a node's Xray process restarts or is restarted, its counters reset. If the counter drops below the stored baseline — including the common case where a node has accumulated traffic and then restarts with some traffic already counted — the delta becomes negative. The pre-fix code handled a negative delta by setting it to canon.Up (the node's full current counter value) rather than 0:

// v3.3.1 — BUGGY
if deltaUp = canon.Up - base.Up; deltaUp < 0 {
    deltaUp = canon.Up  // re-adds the full current counter
}

This caused the node's entire current traffic total to be folded into client_traffics as fresh usage on every backward-counter event. With 8 nodes and multiple Xray restart cycles, these phantom additions accumulate quickly, producing the 20.13 GiB vs 1.45 GiB discrepancy you are seeing.

A secondary issue in the same release: manual traffic resets (ResetClientTrafficByEmail, BulkResetTraffic, resetAllClientTrafficsLocked) cleared client_traffics to zero but did not clear the node_client_traffics baseline rows. If a node had counted traffic it had not yet synced at the moment of the reset, the next sync would apply that unseen delta on top of the freshly zeroed counter.

The fix (PR #5477)

  1. internal/web/service/inbound_node.go: a backward-moving counter is now treated as a reset — delta = 0 and the baseline is rebased to the current counter. No traffic is added; only genuine post-reset accrual counts.
  2. internal/web/service/inbound_traffic.go and internal/web/service/client_traffic.go: all traffic-reset paths now also delete the corresponding node_client_traffics rows so stale pre-reset cumulative values cannot leak back.

How to get the fix now

The fix is in main and will ship in the next tagged release. To apply it today, update to the current main build:

x-ui
# choose option 1 "Update"

Recovering already-affected users

The phantom traffic already recorded in client_traffics will not auto-correct because the table only increases. For the 70 affected users you identified, re-enabling them and resetting their traffic from the panel is the cleanest path. The reset paths (once on the fixed build) will also clear the node baselines, so recounting cannot happen again.

If you need a targeted SQL query to identify affected rows before acting, the script you already wrote (comparing client_traffics total against the SUM across node_client_traffics per email) is the right approach.

The same bug was independently reported and diagnosed in #5456 (which was fixed by the same commit). Your report adds the PostgreSQL multi-node data that confirms the bug affects 70/901 users in this environment.

This reply was generated automatically; a maintainer may follow up.

<!-- gh-comment-id:4769964973 --> @github-actions[bot] commented on GitHub (Jun 22, 2026): The root cause is confirmed and has been fixed in the `main` branch (commit `679d2e1`, PR #5477, merged 2026-06-21). The fix is not yet in a tagged release; v3.3.1 (released 2026-06-12) still carries the bug. **What went wrong** In `internal/web/service/inbound_node.go`, the per-node delta is computed as: ``` delta = node_current_counter - stored_baseline (node_client_traffics) ``` When a node's Xray process restarts or is restarted, its counters reset. If the counter drops below the stored baseline — including the common case where a node has accumulated traffic and then restarts with some traffic already counted — the delta becomes negative. The pre-fix code handled a negative delta by setting it to `canon.Up` (the node's full current counter value) rather than `0`: ```go // v3.3.1 — BUGGY if deltaUp = canon.Up - base.Up; deltaUp < 0 { deltaUp = canon.Up // re-adds the full current counter } ``` This caused the node's entire current traffic total to be folded into `client_traffics` as fresh usage on every backward-counter event. With 8 nodes and multiple Xray restart cycles, these phantom additions accumulate quickly, producing the 20.13 GiB vs 1.45 GiB discrepancy you are seeing. A secondary issue in the same release: manual traffic resets (`ResetClientTrafficByEmail`, `BulkResetTraffic`, `resetAllClientTrafficsLocked`) cleared `client_traffics` to zero but did not clear the `node_client_traffics` baseline rows. If a node had counted traffic it had not yet synced at the moment of the reset, the next sync would apply that unseen delta on top of the freshly zeroed counter. **The fix (PR #5477)** 1. `internal/web/service/inbound_node.go`: a backward-moving counter is now treated as a reset — `delta = 0` and the baseline is rebased to the current counter. No traffic is added; only genuine post-reset accrual counts. 2. `internal/web/service/inbound_traffic.go` and `internal/web/service/client_traffic.go`: all traffic-reset paths now also delete the corresponding `node_client_traffics` rows so stale pre-reset cumulative values cannot leak back. **How to get the fix now** The fix is in `main` and will ship in the next tagged release. To apply it today, update to the current `main` build: ```bash x-ui # choose option 1 "Update" ``` **Recovering already-affected users** The phantom traffic already recorded in `client_traffics` will not auto-correct because the table only increases. For the 70 affected users you identified, re-enabling them and resetting their traffic from the panel is the cleanest path. The reset paths (once on the fixed build) will also clear the node baselines, so recounting cannot happen again. If you need a targeted SQL query to identify affected rows before acting, the script you already wrote (comparing `client_traffics` total against the `SUM` across `node_client_traffics` per email) is the right approach. The same bug was independently reported and diagnosed in #5456 (which was fixed by the same commit). Your report adds the PostgreSQL multi-node data that confirms the bug affects 70/901 users in this environment. *This reply was generated automatically; a maintainer may follow up.*
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#3836
No description provided.