mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[GH-ISSUE #5567] [Bug]: {{TRAFFIC_USED}} and {{{TRAFFIC_LEFT}} variable always displays 0.00B for clients created before v3.4.0 update #3886
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#3886
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 @tradeahadi-cmyk on GitHub (Jun 25, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/5567
Describe the bug
Description
After updating the panel to version 3.4.0, I noticed a backward compatibility issue with the new
{{TRAFFIC_USED}}variable in the subscription template.When using a template like this:
{{INBOUND}} 📊{{TRAFFIC_USED}} / {{TRAFFIC_LEFT}} 📅 Expiry: {{EXPIRE_DATE}}The variable
{{TRAFFIC_USED}}fails to show the correct usage for any client that was created before the v3.4.0 update, always remaining at0.00B.Behavior & Observations
{{TRAFFIC_USED}}variable always displays0.00Bin the subscription link/remark, regardless of their actual usage.{{TRAFFIC_LEFT}}and{{EXPIRE_DATE}}work perfectly for ALL clients (both old and new).up+down) for everyone, meaning the core data exists in the database.What I've Tried (Troubleshooting)
I tried the following actions on the older clients, but none of them fixed the issue:
Suspected Cause
It seems like a database migration/backward compatibility issue. The migration script likely missed recalculating or mapping the existing
upanddownsum into the new field/logic that{{TRAFFIC_USED}}relies on for clients created prior to v3.4.0.How to reproduce the problem
show traffic for old clients
Expected behavior
No response
Actual behavior
No response
Relevant logs
Screenshots
No response
3x-ui version
3.4.0
Xray-core version
No response
How did you install 3x-ui?
install.sh script
Operating system
ubuntu 24
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
@github-actions[bot] commented on GitHub (Jun 25, 2026):
Root cause of
{{TRAFFIC_USED}}always showing0.00Bfor pre-v3.4.0 clientsThe behavior you're seeing is caused by a lookup discrepancy introduced by v3.4.0's new normalized client tables.
How
{{TRAFFIC_USED}}is resolved{{TRAFFIC_USED}}is expanded ininternal/sub/remark_vars.go:189–190:where
stis thexray.ClientTrafficstruct returned bystatsForClient()(line 432):When both Path 1 and Path 2 fail the fallback runs with
Up = 0, Down = 0, which is why{{TRAFFIC_USED}}shows0.00Bwhile{{EXPIRE_DATE}}and{{TRAFFIC_LEFT}}(∞ for unlimited clients) continue to look correct — those come from the client record in the fallback, not from the traffic row.Why both lookup paths fail for old clients
getInboundsBySubId()(internal/sub/service.go) loads inbounds using the newclient_inbounds/clientsjoin tables introduced in v3.4.0, then preloads traffic rows withPreload("ClientStats"), which translates to:The
statsByEmailemail index (Path 2) is built from exactly these same preloaded rows.The constraint: both lookup paths require
client_traffics.inbound_idto match the ID of one of the loaded inbounds.client_traffics.inbound_idis written once whenAddClientStat()first creates the row (internal/web/service/inbound_traffic.go:448–458) and never updated again:UpdateClientStat()(line 461) also skipsinbound_id:If an inbound was deleted and recreated at any point — a common operation when reconfiguring — the new inbound receives a new auto-increment ID. The old
client_traffics.inbound_idis now orphaned. After the v3.4.0 upgrade,getInboundsBySubIdloads the new inbound and itsPreload("ClientStats")finds no rows for the affected clients, so both paths miss and the fallback returns zeros.This also explains why the Clients panel shows correct traffic (
GetAllClientTraffics/AggregateTrafficByEmailsqueryclient_trafficsdirectly by email with noinbound_idfilter) while the subscription remark template does not.Why editing/resetting a client does not help
UpdateClientStat()never updatesinbound_id, so a stale value persists through any number of client edits or traffic resets.x-ui migrate-dbis also not a fix here:MigrationRequirements()(internal/web/service/inbound_migration.go:163–176) only callsAddClientStatwhen a client has no existing row (count == 0). For clients with existing rows — all old clients with any historical traffic — neitherinbound_idnor any field is changed.Diagnostic query
Run against the SQLite DB (stop the panel first; DB is at
/etc/x-ui/x-ui.db):Rows where
old_inbound_gone = 1confirm the orphaned-inbound_idroot cause.Workaround
Back up your database first:
Then, with the panel stopped, open the DB with
sqlite3 /etc/x-ui/x-ui.dband run:This only updates rows whose referenced inbound no longer exists, pointing them to a currently-valid inbound from
client_inbounds. Rows with a still-validinbound_idare left untouched.Proper fix
A permanent fix would modify
statsForClient()to add a direct email-based DB lookup as a last resort before returning the zero-traffic placeholder — the same strategy the panel's Clients page already uses forGetAllClientTraffics().Auto-generated by triage bot — all claims are grounded in source read at commit
e4b881e.@MHSanaei commented on GitHub (Jun 25, 2026):
Fixed in commit
a4be5a0d.Root cause:
{{TRAFFIC_USED}}resolves throughstatsForClient, whose two lookup paths (the inbound's preloadedClientStatsand the per-requeststatsByEmailindex) are both keyed byclient_traffics.inbound_id. That id is written once byAddClientStatand never updated, so an inbound delete+recreate orphans the row from every loaded inbound — both paths miss and the zero-traffic placeholder makes{{TRAFFIC_USED}}read0.00B.{{TRAFFIC_LEFT}}/{{EXPIRE_DATE}}looked fine because they came from the client record in that same placeholder, and the sub-info header was correct because it already aggregates by email.Fix: added a last-resort lookup by the globally-unique
email(cached per request), the same strategy the sub-info header already uses. This is a code-side fix — no manual SQL needed; the variable will report correct usage for pre-v3.4.0 clients after updating. A regression test covering the orphaned-row case is included.