[PR #6003] [MERGED] fix: refresh stale client_traffics row when an inbound-deleted client's email is reused #6053

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

📋 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)

  • ec50490 fix: refresh stale client_traffics row when an inbound-deleted client's email is reused
  • 49dd0fd fix: 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 the client_inbounds link for its clients — it does not delete their clients / client_traffics rows. That's intentional: it mirrors ClientService.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's OnConflict clause was DoNothing on the email column:

return tx.Clauses(clause.OnConflict{Columns: []clause.Column{{Name: "email"}}, DoNothing: true}).
    Create(&clientTraffic).Error

So the orphaned client_traffics row — with the old, possibly disabled/expired/depleted enable / expiry_time / reset / total / inbound_id — survives completely untouched. The new client's config is silently ignored. In practice this means:

  • The client's real usage (up/down, only stored in client_traffics) shows traffic from the previous, unrelated client that happened to share the email.
  • autoRenewClients filters on client_traffics.reset / expiry_time, not the fresh values in clients — 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 DoNothing to a scoped DoUpdates:

return tx.Clauses(clause.OnConflict{
    Columns:   []clause.Column{{Name: "email"}},
    DoUpdates: clause.AssignmentColumns([]string{"inbound_id", "total", "expiry_time", "enable", "reset"}),
}).Create(&clientTraffic).Error

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 single client_traffics row. Every inbound attachment for the same identity calls AddClientStat with the same enable/expiry_time/reset/total (they all come from the same clients row), so refreshing those columns on each call is a no-op in that case — confirmed by TestAddClientStat_MultiInboundReattachStaysIdempotent below.

up/down are 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. at Create time) 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 the client_traffics row picks up the fresh enable/reset/total/expiry_time/inbound_id, while up/down stay 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 real up/down usage, 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:

go test ./internal/web/service/... -v

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.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/6003 **Author:** [@mrnickson-hue](https://github.com/mrnickson-hue) **Created:** 7/16/2026 **Status:** ✅ Merged **Merged:** 7/21/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/client-traffics-stale-on-email-reuse` --- ### 📝 Commits (2) - [`ec50490`](https://github.com/MHSanaei/3x-ui/commit/ec50490d7969de6fbe40b89c48e123616035a3e9) fix: refresh stale client_traffics row when an inbound-deleted client's email is reused - [`49dd0fd`](https://github.com/MHSanaei/3x-ui/commit/49dd0fdec6a351bcb8d9e8448b9f07e6760a95b0) fix: don't let AddClientStat clobber import's forced-enabled ClientStats rows ### 📊 Changes **4 files changed** (+250 additions, -7 deletions) <details> <summary>View changed files</summary> ➕ `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) </details> ### 📄 Description ## Problem Closes #5958. Deleting an inbound (`InboundService.DelInbound`) only removes the `client_inbounds` link for its clients — it does **not** delete their `clients` / `client_traffics` rows. That's intentional: it mirrors `ClientService.Detach`, which does the exact same thing on purpose so a client can be re-`Attach`ed 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`'s `OnConflict` clause was `DoNothing` on the `email` column: ```go return tx.Clauses(clause.OnConflict{Columns: []clause.Column{{Name: "email"}}, DoNothing: true}). Create(&clientTraffic).Error ``` So the orphaned `client_traffics` row — with the old, possibly disabled/expired/depleted `enable` / `expiry_time` / `reset` / `total` / `inbound_id` — survives completely untouched. The new client's config is silently ignored. In practice this means: - The client's real usage (`up`/`down`, only stored in `client_traffics`) shows traffic from the previous, unrelated client that happened to share the email. - `autoRenewClients` filters on `client_traffics.reset` / `expiry_time`, not the fresh values in `clients` — 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 `DoNothing` to a scoped `DoUpdates`: ```go return tx.Clauses(clause.OnConflict{ Columns: []clause.Column{{Name: "email"}}, DoUpdates: clause.AssignmentColumns([]string{"inbound_id", "total", "expiry_time", "enable", "reset"}), }).Create(&clientTraffic).Error ``` 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 single `client_traffics` row. Every inbound attachment for the same identity calls `AddClientStat` with the same `enable`/`expiry_time`/`reset`/`total` (they all come from the same `clients` row), so refreshing those columns on each call is a no-op in that case — confirmed by `TestAddClientStat_MultiInboundReattachStaysIdempotent` below. `up`/`down` are **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. at `Create` time) 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 the `client_traffics` row picks up the fresh `enable`/`reset`/`total`/`expiry_time`/`inbound_id`, while `up`/`down` stay 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 real `up`/`down` usage, then re-`Create`s the same client onto a second inbound, and asserts the usage counters and config columns are unchanged. Full suite, to check for regressions elsewhere: ``` go test ./internal/web/service/... -v ``` All packages pass (`service`, `email`, `integration`, `outbound`, `panel`, `tgbot`), no failures. `go vet ./internal/web/service/...` is clean. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:06:02 +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#6053
No description provided.