mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[GH-ISSUE #5456] [Bug]: Node restart re-adds the node's full cumulative per-client traffic to master counters (double-counting) #3816
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#3816
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?
Originally created by @Mohamad82Bz on GitHub (Jun 20, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/5456
Describe the bug
When a sub-node goes offline and reconnects, the master panel adds the node's entire cumulative per-client traffic counter onto each client's master total a second time, instead of applying only the delta since the last successful sync. Many clients jumped by roughly their full lifetime usage, pushing them over quota and auto-disabling them.
One of my two sub-nodes (
iran-1) rebooted. In the same ~5-minute sync window that the node came back online, 50 clients' counters spiked by a combined ~2,560 GB. For nearly every affected client, the amount added equals that node's stored cumulativeup+downfor the same client (node_client_traffics) — the tell that the node's full counter was folded in as new usage.All panels run the
mainbuild from commit37c5e0b("feat(node): node hardening — mTLS, hashed+zstd reconcile transport"), so the reconcile transport is a likely suspect.How to reproduce the problem
Expected behavior
On reconnect, the master should reconcile against the last-known baseline for that node and add only the delta (new - last-seen) to each client total. A node restart should never increase a client's total by more than the traffic actually used.
Actual behavior
The master added the node's full cumulative per-client counter again, in one step, in the sync window right after the node reconnected.
Evidence (per-client traffic sampled every 5 min; times IRST/UTC+3:30):
Phantom amount added vs the rebooted node's stored cumulative up+down (names redacted):
Both up and down counters were inflated (not just total), consistent with the node's full up/down being re-applied.
Relevant logs
Screenshots
No response
3x-ui version
main build
37c5e0b(CI run 27610742946, 2026-06-16) - after 3.3.1Xray-core version
26.6.1
How did you install 3x-ui?
install.sh script
Operating system
Ubuntu 24.04
Which parts of the panel are affected?
No response
Browser (only if it is a UI bug)
N/A (not a UI bug)
Is the panel behind a reverse proxy or CDN?
None
Before submitting
@github-actions[bot] commented on GitHub (Jun 20, 2026):
The report is well-evidenced and the data points to a concrete accounting bug. Here is what the source shows and what additional information is needed to reproduce and fix it.
How the delta accounting works
setRemoteTrafficLocked(internal/web/service/inbound_node.go) maintains a per-(node, email) baseline in thenode_client_trafficstable. On every sync it computes:where
canon.Upis the maximum reported by any inbound on that node for that email (thenodeEmailTotalsmax-fold added for #5274). The baseline is then immediately updated tocanon.Upso the next cycle starts fresh.A "reset clamp" guards against counter resets: if
delta < 0(the node counter went down, e.g. because the node's Xray or DB was wiped), the code substitutescanon.Upas the delta instead of a negative number.What the evidence implies
Your table shows that, for every affected client, the amount added equals
node_client_traffics.up + node_client_traffics.down. After a sync,node_client_trafficsis always overwritten with the current node counter, so the "stored up+down" you are reading is the new baseline written during the buggy sync — which equalscanon.Up + canon.Downat that moment.For the delta to equal the full current counter:
The old baseline must have been zero (a row existed in
node_client_trafficswithup = 0, down = 0). A missing baseline (\!seen) would yielddelta = 0, so the row was present but held zero.When does a baseline become zero
upsertNodeBaseline(inbound_node.go:172) is the only path that writesnode_client_trafficsvalues. It always writes the currentcanon.Up / canon.Down. A baseline of zero therefore requires that the master successfully synced this node while the node was reporting 0 for those clients. This can happen in at least two ways:Traffic-reset while node is online: A "Reset traffic" action for the client is propagated to the node via
rt.ResetClientTraffic(inbound_traffic.go:617-619), setting the node'sclient_traffics.up = 0. If the master's next 5-second sync runs before the node has re-accumulated meaningful traffic, it writesbaseline = 0. If the node then goes offline (reboot), it may accumulate hundreds of GB before reconnecting — the master sees that full amount against a zero baseline.Fresh install or DB wipe on the node during a brief reachable window: If the node's
client_trafficstable was empty (e.g. a fresh panel install overwrote the DB), the/panel/api/inbounds/listresponse will haveClientStatswithup = 0(or no stats at all, whichenrichClientStatssynthesizes as zero). A sync at this moment writesbaseline = 0. If the node then reboots again with the old database restored (or the old database was preserved but the DB migration reset the counter rows), the next sync sees the historical value against the zero baseline.The reset-clamp is meant to handle counter decreases, but it only adds the current counter as new traffic (
deltaUp = canon.Up) — so a zero baseline plus a large current counter produces exactly the reported spike.Why the reconcile transport may be involved
The mTLS / zstd reconcile transport (
internal/web/middleware/config_envelope.go,internal/web/runtime/remote.go:do) does not alter traffic or baseline logic. However, a node freshly reconnecting withConfigDirty = truerunsReconcileNodebefore the first traffic snapshot.ReconcileNodecallsUpdateInboundon the node (remote.go:457), which triggersupdateClientTrafficson the node side. For clients in the current inbound settings but not yet inclient_trafficson the node (e.g. after a DB wipe or fresh install),AddClientStatinserts a row withup = 0, down = 0. The immediately-followingFetchTrafficSnapshotwould then show those rows as zero — triggeringupsertNodeBaseline(0, 0)if the master has anode_client_trafficsrow with any non-zero value from a previous run (race direction can reverse). This is speculative; logs would confirm or eliminate it.Diagnostic steps
Please collect the following from the master panel around 22:37–22:43 IRST on 2026-06-20:
Master panel log (enable debug level if not already set via
XUI_LOG_LEVEL=debugin/etc/default/x-ui). Look for lines containing"node traffic sync"and the name iran-1 from roughly 21:00 onward. In particular:"node traffic sync: fetch from iran-1 failed"or"merge for iran-1 failed"repeatedly before the spike?"node traffic sync: reconcile for iran-1 failed"or"node traffic sync: reconcile for iran-1"just before the spike?Immediately after discovering the spike, run on the master:
This shows the current (post-spike) baselines. They should now equal the node's counter. Also check:
On the node (iran-1), check whether the panel's database was ever at zero for these clients:
And confirm the node's current Xray uptime vs. its
client_trafficsaccumulation to determine whether the traffic is from before or after the most recent reboot.Confirm: was a "Reset traffic" action performed for any of the affected clients (reverse-A, reverse-B, etc.) within the past few days or weeks while iran-1 was online?
Confirm: did iran-1 undergo a fresh install or DB migration at any point recently, or was this purely a power-cycle reboot with no software changes?
Relationship to prior issues
This is not a duplicate of #5202 (which is about new clients whose baseline commit fails due to network timeouts causing repeated compounding while the client is online) or of #5190 (PostgreSQL node-replacement data inconsistency). The failure mode here — one large spike on reconnect — is distinct and occurs for established clients, not just new ones. It belongs to the same general category of delta-accounting failures.
This reply was generated automatically by an issue-triage assistant; a maintainer may follow up with additional context or a fix.
@Mohamad82Bz commented on GitHub (Jun 21, 2026):
<html> <html><head></head>Thanks for the detailed analysis — it matches what I can reconstruct from the data, and I can confirm several of your points directly.
Logs: Unfortunately I don't have them for the event. Debug logging was not enabled at the time (I've enabled it now for any recurrence), and there were no relevant
journalctllines retained either. So I can't supply the master sync/reconcile log lines. Everything below is reconstructed from a master DB backup taken ~01.5h after the spike plus a per-client traffic time-series sampled every 5 minutes.1. The prior baseline was effectively zero — confirmed.
You noted that for
delta = canon.Up - baseline.Upto equal the full current counter, the old baseline must have been ~0, and that the "stored up+down" in my original table is the new baseline written during the buggy sync. Correct on both counts — my backup is post-spike.I computed the implied old baseline per field as
canon(post-spike node_client_traffics) − delta(applied at spike):So the baseline used in the delta was effectively zero, exactly as your reasoning requires.
2. The zeroing was per-field, not a clean whole-row wipe.
A few clients had only one component zeroed. Example (redacted):
For
reverse-Bthe upload baseline was intact (2,789.7 GB, nothing added) while only the download baseline went to zero. This looks like a snapshot-timing effect rather than a single row being cleared.3. Evidence points away from a per-client "Reset traffic" (your Scenario 1).
This is most consistent with your "speculative" reconcile path — a
ConfigDirtynode runningReconcileNode/UpdateInboundbefore the first traffic snapshot, zeroing baselines node-wide on reconnect.4. Timing lines up exactly.
iran-1's uptime at backup was 5,185 s, placing its boot at ~22:40 IRST. The spike interval is 22:37:54 → 22:42:54 IRST. The first post-reboot sync is the spike.
5. Honest limitation.
From totals alone I can't distinguish "the
node_client_trafficsrow literally held 0" from the reset-clamp firing on a negative delta — both yieldadded = current counterand both compute back to an implied-zero baseline. Only the master logs would separate these, and I don't have them for this event.Answers to your diagnostic questions:
So: established clients, pure reboot, node-wide simultaneous baseline-zeroing on reconnect, applied as one large delta. If it recurs I now have debug logging enabled and will attach the
</html> </html>node traffic sync/reconcile for iran-1lines.