[PR #6110] [MERGED] fix: stop deleting client_traffics for detached-but-alive clients #6084

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6110
Author: @mrnickson-hue
Created: 7/26/2026
Status: ✅ Merged
Merged: 7/28/2026
Merged by: @MHSanaei

Base: main ← Head: fix/6102-orphaned-traffics-and-summary


📝 Commits (2)

  • 4a47c74 fix: stop deleting client_traffics for detached-but-alive clients
  • 07d13fc fix: union both keep-sets instead of replacing (review feedback)

📊 Changes

5 files changed (+112 additions, -18 deletions)

View changed files

📝 frontend/src/hooks/useClients.ts (+17 -10)
📝 frontend/src/pages/clients/ClientsPage.tsx (+1 -4)
📝 frontend/src/test/clients-summary.test.ts (+26 -2)
📝 internal/web/service/inbound_migration.go (+9 -2)
📝 internal/web/service/inbound_migration_test.go (+59 -0)

📄 Description

Summary

Fixes two related bugs behind the reported "Ended/Disabled clients hidden" symptom: a migration that deletes client_traffics rows for clients live in only one of the two places that can now hold "this client still exists" (the clients table or an inbound's JSON), and a frontend live-recompute that silently drops any client missing a traffic row out of every summary bucket.

Why

Closes #6102.

The reporter's screenshot shows Clients 67 but Ended 0 + Depleting 0 + Disabled 0 + Active 58 = 58 — the buckets don't sum to the total, and the hover popovers that normally list matching emails are empty, leaving the Filter drawer as the only way to reach those clients.

Root cause has two independent parts:

  1. MigrationRemoveOrphanedTraffics (internal/web/service/inbound_migration.go) originally keyed "orphaned" off a client's absence from every inbound's settings.clients[] JSON — a definition that predates #4469's standalone clients table. ClientService.Detach intentionally keeps a client's client_traffics row when it drops its last inbound attachment (so the client can be re-attached later without losing stats/expiry), but such a client has no entry in any inbound's JSON anymore. Every x-ui migrate run or backup restore (MigrateDB) therefore deleted its traffic row anyway, even though the client itself was untouched and still listed — reproducing the exact "row exists, — under Attached inbounds, invisible to the summary" state from the screenshot. My first pass at this PR simply switched the predicate to key off the clients table instead — an automated review on this PR (see the thread below) caught that this trades one gap for a worse one: the one-shot ClientsTable seeder (internal/database/db.go) skips a client it fails to unmarshal and never retries, so a client that's still live in an inbound's JSON can have no clients row at all, and an empty clients table (edge case, but reachable) would delete every client_traffics row outright. Fixed by unioning both keep-sets — a row survives if it's referenced by either the clients table or any inbound's JSON, and is only removed when it's in neither.
  2. frontend/src/hooks/useClients.ts recomputed the clients summary from the client_stats WebSocket snapshot as soon as it arrived, without checking whether that snapshot actually covered every client. allClientStats is client_traffics rows (email-keyed), so any client missing a row for the reason above (or any other reason) silently fell out of every bucket in the recompute while the headline total — kept from the server — still counted it. The added guard falls back to the server summary whenever the snapshot is shorter than the server's total; note this is a count comparison, not an exact coverage check (a genuinely orphaned client_traffics row — the case #1 prunes, only on migrate/restore — could in theory offset a missing one and let a short snapshot slip past the guard), and it's a deliberately visible trade: when it does trip, the summary cards stop updating live and only refresh on the next list poll, rather than risk showing an undercount.

I went through the whole master/node sync path (setRemoteTrafficLocked) separately, since the reporter also described a master+node deployment where clients "completely disappear" after depleting/being disabled there. I could not reproduce that through that path — disableInvalidClients only flips enable=false both in the DB row and inside the settings JSON, the client stays in the array, and every deletion branch in the node sync is already guarded against exactly this kind of false-positive sweep. Left a comment on the issue asking the reporter to confirm whether x-ui migrate or a backup restore happened around when they started seeing it, since that would point at the same bug from a different, more surprising trigger.

Type of change

  • Bug fix

Areas affected

  • Frontend (UI / panel pages)
  • Backend (API endpoints, login, settings)
  • Statistics / traffic counters
  • Database / migrations
  • Multi-node (sub-nodes) — investigated, not changed (see above)

How was this tested?

  • TestMigrationRemoveOrphanedTraffics (internal/web/service/inbound_migration_test.go) covers all four combinations via real code paths, not hand-built rows where a real path exists: a client attached normally (ClientService.SyncInbound), a client detached from every inbound but kept alive (driven through the actual ClientService.Detach(keepTraffic=true) call), a client present in an inbound's JSON with no clients row (the seeder-gap case, built by hand since no production path currently produces it on purpose), and a truly orphaned row in neither. Confirmed each case fails against the wrong predicate (the original bug, and separately the regression from unioning-instead-of-replacing) and passes with the fix.
  • Three new cases in frontend/src/test/clients-summary.test.ts for the extracted pickClientsSummary: falls back to the server summary when the snapshot is shorter than the server's total, uses the live recompute when the snapshot covers every client, and falls back before the first WS snapshot arrives.
  • go build ./..., go vet ./..., gofmt -l / goimports -l on the changed files — clean. (golangci-lint itself couldn't run in my sandbox — the resolvable release is built against Go 1.25 and refuses to lint a go.mod targeting 1.26.5 — but gofmt/goimports cover the formatting side of it.)
  • go test ./... and go test -race ./internal/web/service/... — all green.
  • Frontend: npm run typecheck, npm run lint, full npm run test (890 tests), npm run build, npm run build-storybook — all green.

Screenshots / recordings

N/A — this is a data-correctness fix, not a visual change. The summary cards' appearance is unchanged; they just stop undercounting (and, in the rare short-snapshot case described above, stop live-updating until the next poll instead).

Breaking changes

None. MigrationRemoveOrphanedTraffics still runs automatically on x-ui migrate / backup restore; it just no longer deletes rows it shouldn't.

Checklist

  • I tested the change locally and confirmed the described behavior.
  • I added or updated tests for the new behavior.
  • go build ./... and the test suite pass locally.
  • For frontend changes: npm run lint, npm run typecheck, and npm run build pass.
  • I updated the Wiki / README / API docs if user-facing behavior changed. — N/A, no user-facing behavior/docs to update.
  • My commits follow the project's existing message style.
  • I have no unrelated changes mixed into this PR.

🔄 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/6110 **Author:** [@mrnickson-hue](https://github.com/mrnickson-hue) **Created:** 7/26/2026 **Status:** ✅ Merged **Merged:** 7/28/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/6102-orphaned-traffics-and-summary` --- ### 📝 Commits (2) - [`4a47c74`](https://github.com/MHSanaei/3x-ui/commit/4a47c74e7d2a6ca43d06b045866fc4e3c25148f4) fix: stop deleting client_traffics for detached-but-alive clients - [`07d13fc`](https://github.com/MHSanaei/3x-ui/commit/07d13fc2305bfbc0263fbba03af4cc13efc90540) fix: union both keep-sets instead of replacing (review feedback) ### 📊 Changes **5 files changed** (+112 additions, -18 deletions) <details> <summary>View changed files</summary> 📝 `frontend/src/hooks/useClients.ts` (+17 -10) 📝 `frontend/src/pages/clients/ClientsPage.tsx` (+1 -4) 📝 `frontend/src/test/clients-summary.test.ts` (+26 -2) 📝 `internal/web/service/inbound_migration.go` (+9 -2) 📝 `internal/web/service/inbound_migration_test.go` (+59 -0) </details> ### 📄 Description ## Summary Fixes two related bugs behind the reported "Ended/Disabled clients hidden" symptom: a migration that deletes `client_traffics` rows for clients live in only one of the two places that can now hold "this client still exists" (the `clients` table or an inbound's JSON), and a frontend live-recompute that silently drops any client missing a traffic row out of every summary bucket. ## Why Closes #6102. The reporter's screenshot shows `Clients 67` but `Ended 0 + Depleting 0 + Disabled 0 + Active 58 = 58` — the buckets don't sum to the total, and the hover popovers that normally list matching emails are empty, leaving the Filter drawer as the only way to reach those clients. Root cause has two independent parts: 1. **`MigrationRemoveOrphanedTraffics` (`internal/web/service/inbound_migration.go`)** originally keyed "orphaned" off a client's absence from every inbound's `settings.clients[]` JSON — a definition that predates #4469's standalone `clients` table. `ClientService.Detach` intentionally keeps a client's `client_traffics` row when it drops its last inbound attachment (so the client can be re-attached later without losing stats/expiry), but such a client has no entry in any inbound's JSON anymore. Every `x-ui migrate` run or backup restore (`MigrateDB`) therefore deleted its traffic row anyway, even though the client itself was untouched and still listed — reproducing the exact "row exists, `—` under Attached inbounds, invisible to the summary" state from the screenshot. My first pass at this PR simply switched the predicate to key off the `clients` table instead — an automated review on this PR (see the thread below) caught that this trades one gap for a worse one: the one-shot `ClientsTable` seeder (`internal/database/db.go`) skips a client it fails to unmarshal and never retries, so a client that's still live in an inbound's JSON can have no `clients` row at all, and an empty `clients` table (edge case, but reachable) would delete every `client_traffics` row outright. Fixed by unioning both keep-sets — a row survives if it's referenced by either the `clients` table or any inbound's JSON, and is only removed when it's in neither. 2. **`frontend/src/hooks/useClients.ts`** recomputed the clients summary from the `client_stats` WebSocket snapshot as soon as it arrived, without checking whether that snapshot actually covered every client. `allClientStats` is `client_traffics` rows (email-keyed), so any client missing a row for the reason above (or any other reason) silently fell out of every bucket in the recompute while the headline `total` — kept from the server — still counted it. The added guard falls back to the server summary whenever the snapshot is shorter than the server's total; note this is a count comparison, not an exact coverage check (a genuinely orphaned `client_traffics` row — the case #1 prunes, only on migrate/restore — could in theory offset a missing one and let a short snapshot slip past the guard), and it's a deliberately visible trade: when it does trip, the summary cards stop updating live and only refresh on the next list poll, rather than risk showing an undercount. I went through the whole master/node sync path (`setRemoteTrafficLocked`) separately, since the reporter also described a master+node deployment where clients "completely disappear" after depleting/being disabled there. I could not reproduce that through that path — `disableInvalidClients` only flips `enable=false` both in the DB row and inside the settings JSON, the client stays in the array, and every deletion branch in the node sync is already guarded against exactly this kind of false-positive sweep. Left a comment on the issue asking the reporter to confirm whether `x-ui migrate` or a backup restore happened around when they started seeing it, since that would point at the same bug from a different, more surprising trigger. ## Type of change - [x] Bug fix ## Areas affected - [x] Frontend (UI / panel pages) - [x] Backend (API endpoints, login, settings) - [x] Statistics / traffic counters - [x] Database / migrations - [x] Multi-node (sub-nodes) — investigated, not changed (see above) ## How was this tested? - `TestMigrationRemoveOrphanedTraffics` (`internal/web/service/inbound_migration_test.go`) covers all four combinations via real code paths, not hand-built rows where a real path exists: a client attached normally (`ClientService.SyncInbound`), a client detached from every inbound but kept alive (driven through the actual `ClientService.Detach(keepTraffic=true)` call), a client present in an inbound's JSON with no `clients` row (the seeder-gap case, built by hand since no production path currently produces it on purpose), and a truly orphaned row in neither. Confirmed each case fails against the wrong predicate (the original bug, and separately the regression from unioning-instead-of-replacing) and passes with the fix. - Three new cases in `frontend/src/test/clients-summary.test.ts` for the extracted `pickClientsSummary`: falls back to the server summary when the snapshot is shorter than the server's total, uses the live recompute when the snapshot covers every client, and falls back before the first WS snapshot arrives. - `go build ./...`, `go vet ./...`, `gofmt -l` / `goimports -l` on the changed files — clean. (`golangci-lint` itself couldn't run in my sandbox — the resolvable release is built against Go 1.25 and refuses to lint a go.mod targeting 1.26.5 — but gofmt/goimports cover the formatting side of it.) - `go test ./...` and `go test -race ./internal/web/service/...` — all green. - Frontend: `npm run typecheck`, `npm run lint`, full `npm run test` (890 tests), `npm run build`, `npm run build-storybook` — all green. ## Screenshots / recordings N/A — this is a data-correctness fix, not a visual change. The summary cards' appearance is unchanged; they just stop undercounting (and, in the rare short-snapshot case described above, stop live-updating until the next poll instead). ## Breaking changes None. `MigrationRemoveOrphanedTraffics` still runs automatically on `x-ui migrate` / backup restore; it just no longer deletes rows it shouldn't. ## Checklist - [x] I tested the change locally and confirmed the described behavior. - [x] I added or updated tests for the new behavior. - [x] `go build ./...` and the test suite pass locally. - [x] For frontend changes: `npm run lint`, `npm run typecheck`, and `npm run build` pass. - [ ] I updated the Wiki / README / API docs if user-facing behavior changed. — N/A, no user-facing behavior/docs to update. - [x] My commits follow the project's existing message style. - [x] I have no unrelated changes mixed into this PR. --- <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:14 +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#6084
No description provided.