mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 19:11:53 +07:00
[PR #6003] [MERGED] fix: refresh stale client_traffics row when an inbound-deleted client's email is reused #6053
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#6053
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?
📋 Pull Request Information
Original PR: https://github.com/MHSanaei/3x-ui/pull/6003
Author: @mrnickson-hue
Created: 7/16/2026
Status: ✅ Merged
Merged: 7/21/2026
Merged by: @MHSanaei
Base:
main← Head:fix/client-traffics-stale-on-email-reuse📝 Commits (2)
ec50490fix: refresh stale client_traffics row when an inbound-deleted client's email is reused49dd0fdfix: don't let AddClientStat clobber import's forced-enabled ClientStats rows📊 Changes
4 files changed (+250 additions, -7 deletions)
View changed files
➕
internal/web/service/client_stat_reuse_test.go(+177 -0)📝
internal/web/service/inbound.go(+10 -0)📝
internal/web/service/inbound_import_shared_clients_test.go(+34 -0)📝
internal/web/service/inbound_traffic.go(+29 -7)📄 Description
Problem
Closes #5958.
Deleting an inbound (
InboundService.DelInbound) only removes theclient_inboundslink for its clients — it does not delete theirclients/client_trafficsrows. That's intentional: it mirrorsClientService.Detach, which does the exact same thing on purpose so a client can be re-Attached later with its accumulated traffic intact.The problem is what happens if that same email is instead reused for a freshly (re)created client on a different inbound, via
ClientService.Create.AddClientStat'sOnConflictclause wasDoNothingon theemailcolumn:So the orphaned
client_trafficsrow — with the old, possibly disabled/expired/depletedenable/expiry_time/reset/total/inbound_id— survives completely untouched. The new client's config is silently ignored. In practice this means:up/down, only stored inclient_traffics) shows traffic from the previous, unrelated client that happened to share the email.autoRenewClientsfilters onclient_traffics.reset/expiry_time, not the fresh values inclients— so auto-renew can silently never trigger for the "new" client, because it's still reading the old row.Full repro / investigation notes are in the issue.
Fix
Change the conflict path from
DoNothingto a scopedDoUpdates:This is safe for the legitimate case
AddClientStat's conflict handling also has to serve — a client attached to more than one inbound shares a singleclient_trafficsrow. Every inbound attachment for the same identity callsAddClientStatwith the sameenable/expiry_time/reset/total(they all come from the sameclientsrow), so refreshing those columns on each call is a no-op in that case — confirmed byTestAddClientStat_MultiInboundReattachStaysIdempotentbelow.up/downare deliberately excluded from the refresh — they're the accumulated traffic counters, and zeroing them here would erase real usage every time an existing, actively-used client is attached to one more inbound.Known limitation (not fully solved by this change, flagged in the code comment): a genuinely new client that happens to reuse an orphaned email still inherits that row's leftover
up/down, since nothing at this call site can distinguish "this is the same logical identity re-attaching" from "this is an unrelated new client that picked the same email." Fixing that would need to detect the reuse case explicitly (e.g. atCreatetime) and is out of scope for this narrow, low-risk fix.Testing
Added
internal/web/service/client_stat_reuse_test.go:TestAddClientStat_RefreshesStaleRowOnInboundDeleteThenReuse— full repro: create a client, disable it, delete its only inbound (orphaning it), then re-create the same email/subId on a new inbound with different settings. Asserts theclient_trafficsrow picks up the freshenable/reset/total/expiry_time/inbound_id, whileup/downstay untouched. Verified this test fails without the fix (reproduces the exact stale values reported in #5958) and passes with it.TestAddClientStat_MultiInboundReattachStaysIdempotent— guards the legitimate multi-inbound-share case: seeds realup/downusage, then re-Creates the same client onto a second inbound, and asserts the usage counters and config columns are unchanged.Full suite, to check for regressions elsewhere:
All packages pass (
service,email,integration,outbound,panel,tgbot), no failures.go vet ./internal/web/service/...is clean.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.