[GH-ISSUE #6286] [Bug]: client IP entries never expire once a client stops connecting #4269

Closed
opened 2026-10-31 20:38:25 +07:00 by bsakaguchi · 2 comments
Owner

Originally created by @rokokol on GitHub (Aug 22, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6286

Describe the bug

The 30-minute eviction of stored client IPs is intentional: PR #4321 proposed keeping the history and was closed with "the 30-minute eviction is intentional; without it the per-client IP blob grows unbounded". That invariant does not hold in practice, because the eviction is a filter applied while a row is being rewritten, and a row is only rewritten when its client shows up in the current online-stats scan.

CheckClientIpJob.processObserved loads rows with where client_email IN (observed emails), and nothing ever visits the rest of the table. So the moment a client stops connecting, its last blob freezes with the addresses and timestamps it had at that point, and it is kept indefinitely — mergeClientIps never runs for that row again, so ipStaleAfterSeconds stops being an upper bound on retention.

The same applies to node_client_ips, written by recordLocalAttribution. On top of that, Run returns early when fail2ban is not enabled, so on those panels nothing is pruned at all while previously written rows stay where they are.

How to reproduce the problem

  1. Let a client connect so that its inbound_client_ips row fills with (ip, timestamp) entries
  2. Disconnect that client and do not reconnect it
  3. Wait more than 30 minutes
  4. Run select client_email, ips from inbound_client_ips; — the row still carries the same addresses with their original timestamps, and it will keep them indefinitely

Expected behavior

No stored address outlives ipStaleAfterSeconds, whether or not its client is currently online. The retention rule stated in #4321 should hold for every row, not only for the rows that happen to be rewritten.

Actual behavior

Rows belonging to inactive clients retain their addresses forever. On one of our nodes the stored entries were between 32 and 38 hours old at the time of the query, all of them for clients that were simply offline. In a related case (#6285) rows survived eight days after the node they belonged to changed its panelGuid.

The practical effect is that a panel accumulates client IP addresses far beyond the retention the code intends, and there is no supported way to inspect how much is kept or to clear it.

A possible fix would be to apply the same cutoff in a periodic sweep: every few minutes, prune every row regardless of observation, dropping entries older than the cutoff and deleting rows that end up empty. Both tables hold one row per client per guid, so a full pass is cheap next to the job's own 10-second cadence.

Relevant logs

Emails are replaced with placeholders and addresses are removed; only the timestamps matter here.

-- current time when the query below was run: 1787393116 = 2026-08-22 10:05:16 UTC
sqlite> select email, ips from node_client_ips where length(ips) > 2;
client-a|[{"ip":"<redacted>","timestamp":1787257835}]   -- 2026-08-20 20:30:35 UTC, 37.6 h old
client-b|[{"ip":"<redacted>","timestamp":1787275855}]   -- 2026-08-21 01:30:55 UTC, 32.6 h old
client-c|[{"ip":"<redacted>","timestamp":1787260945}]   -- 2026-08-20 21:22:25 UTC, 36.7 h old

All three clients were offline for that whole period, so their rows were never revisited and the 30-minute cutoff was never applied to them.

3x-ui version

3.6.0

Xray-core version

26.7.28

How did you install 3x-ui?

install.sh script

Operating system

Ubuntu 24.04.4 LTS

Which parts of the panel are affected?

Database / migrations, Multi-node (sub-nodes)

Before submitting

  • I searched existing issues and this bug has not been reported.
  • I am running the latest released version of 3x-ui (or have verified the bug still exists on it).
  • This bug report is written in English.
  • I have redacted any sensitive data (tokens, real domains, client UUIDs).
Originally created by @rokokol on GitHub (Aug 22, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6286 ### Describe the bug The 30-minute eviction of stored client IPs is intentional: PR #4321 proposed keeping the history and was closed with "the 30-minute eviction is intentional; without it the per-client IP blob grows unbounded". That invariant does not hold in practice, because the eviction is a filter applied while a row is being rewritten, and a row is only rewritten when its client shows up in the current online-stats scan. `CheckClientIpJob.processObserved` loads rows with `where client_email IN (observed emails)`, and nothing ever visits the rest of the table. So the moment a client stops connecting, its last blob freezes with the addresses and timestamps it had at that point, and it is kept indefinitely — `mergeClientIps` never runs for that row again, so `ipStaleAfterSeconds` stops being an upper bound on retention. The same applies to `node_client_ips`, written by `recordLocalAttribution`. On top of that, `Run` returns early when fail2ban is not enabled, so on those panels nothing is pruned at all while previously written rows stay where they are. ### How to reproduce the problem 1. Let a client connect so that its `inbound_client_ips` row fills with (ip, timestamp) entries 2. Disconnect that client and do not reconnect it 3. Wait more than 30 minutes 4. Run `select client_email, ips from inbound_client_ips;` — the row still carries the same addresses with their original timestamps, and it will keep them indefinitely ### Expected behavior No stored address outlives `ipStaleAfterSeconds`, whether or not its client is currently online. The retention rule stated in #4321 should hold for every row, not only for the rows that happen to be rewritten. ### Actual behavior Rows belonging to inactive clients retain their addresses forever. On one of our nodes the stored entries were between 32 and 38 hours old at the time of the query, all of them for clients that were simply offline. In a related case (#6285) rows survived eight days after the node they belonged to changed its `panelGuid`. The practical effect is that a panel accumulates client IP addresses far beyond the retention the code intends, and there is no supported way to inspect how much is kept or to clear it. A possible fix would be to apply the same cutoff in a periodic sweep: every few minutes, prune every row regardless of observation, dropping entries older than the cutoff and deleting rows that end up empty. Both tables hold one row per client per guid, so a full pass is cheap next to the job's own 10-second cadence. ### Relevant logs Emails are replaced with placeholders and addresses are removed; only the timestamps matter here. ``` -- current time when the query below was run: 1787393116 = 2026-08-22 10:05:16 UTC sqlite> select email, ips from node_client_ips where length(ips) > 2; client-a|[{"ip":"<redacted>","timestamp":1787257835}] -- 2026-08-20 20:30:35 UTC, 37.6 h old client-b|[{"ip":"<redacted>","timestamp":1787275855}] -- 2026-08-21 01:30:55 UTC, 32.6 h old client-c|[{"ip":"<redacted>","timestamp":1787260945}] -- 2026-08-20 21:22:25 UTC, 36.7 h old ``` All three clients were offline for that whole period, so their rows were never revisited and the 30-minute cutoff was never applied to them. ### 3x-ui version 3.6.0 ### Xray-core version 26.7.28 ### How did you install 3x-ui? install.sh script ### Operating system Ubuntu 24.04.4 LTS ### Which parts of the panel are affected? Database / migrations, Multi-node (sub-nodes) ### Before submitting - [x] I searched existing issues and this bug has not been reported. - [x] I am running the latest released version of 3x-ui (or have verified the bug still exists on it). - [x] This bug report is written in English. - [x] I have redacted any sensitive data (tokens, real domains, client UUIDs).
bsakaguchi 2026-10-31 20:38:25 +07:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@github-actions[bot] commented on GitHub (Aug 22, 2026):

Confirmed — this is a real bug and it is still present on the default branch (1250fbb7). Your reading of the mechanism is correct: ipStaleAfterSeconds is only ever applied while a row is being rewritten, and only rows whose email appears in the current online-stats scan are ever rewritten.

Root cause

CheckClientIpJob.processObserved derives its entire work list from the scan:

// internal/web/job/check_client_ip_job.go:274-278
emails := make([]string, 0, len(observed))
for email := range observed {
    emails = append(emails, email)
}

loadClientIpRows then narrows the table to exactly those emails —
db.Where("client_email IN ?", batch).Find(&rows) (check_client_ip_job.go:251) — and the loop at :307 iterates emails only. The cutoff itself lives one layer further in, inside the rewrite:

// internal/web/job/check_client_ip_job.go:519
ipMap := mergeClientIps(oldIpsWithTime, newIpsWithTime, time.Now().Unix()-ipStaleAfterSeconds, observedAreLive)

So a row whose client stops connecting is never selected, never merged, and never pruned. ipStaleAfterSeconds (check_client_ip_job.go:45, int64(30 * 60)) is a cutoff applied on write, not a retention bound.

node_client_ips has the same shape, and upsertNodeClientIps makes it especially visible: it already loads every row for the guid at internal/web/service/inbound_node_ips.go:58-61, then throws that away and iterates only the incoming emails —

// internal/web/service/inbound_node_ips.go:69-75, 84
emails := make([]string, 0, len(perEmail))
for email := range perEmail { ... }
sort.Strings(emails)
...
for _, email := range emails {

— so the len(merged) == 0 delete at :90-101 can only ever fire for a client that is still being observed.

This is not a regression. The cutoff has been applied inside the merge since eef2d311 ("Fix IP Limit continuous ban loop from stale DB entries (#4077)"), and node_client_ips inherited that shape when it was added in 9385b6c6.

Two things your report did not have

1. The flat table is partly swept on a master/node pair; node_client_ips is not. node_traffic_sync_job.go:436-454 ships the whole inbound_client_ips table in both directions (FetchAllClientIps → MergeInboundClientIps, then PushAllClientIps back), and MergeInboundClientIps applies the cutoff to every incoming row (inbound_client_ips.go:80,137). That gives inbound_client_ips an indirect full-table pass whenever IP sync runs. node_client_ips gets no equivalent, because GetClientIpsByGuid already drops stale entries and skips empty rows before they leave the node (inbound_node_ips.go:161-174) — the master therefore never sees a stale attribution row to prune. That is almost certainly why your evidence came out of node_client_ips rather than inbound_client_ips. On a standalone panel with no nodes, neither table is swept at all.

2. Deleted and renamed clients leave node_client_ips rows behind permanently. Client deletion removes the flat row and the node traffic row but not the attribution row:

// internal/web/service/client_crud.go:844-849
if err := db.Where("client_email = ?", email).Delete(&model.InboundClientIps{}).Error; err != nil { ... }
if err := db.Where("email = ?", email).Delete(&model.NodeClientTraffic{}).Error; err != nil { ... }

There is no model.NodeClientIp delete on that path, and UpdateClientIPs (inbound_client_ips.go:154-163) re-keys only the flat table on rename. Since nothing ever revisits an unobserved row, those become permanent orphans — that is row-count growth, not just frozen timestamps, and it strengthens the case beyond the retention argument.

On your other points

  • The Run early return is real. check_client_ip_job.go:62-64 returns before processObserved when XUI_ENABLE_FAIL2BAN is set to anything other than true (isFail2BanEnabled, :472-475; default is enabled, .env.example:69). Worth noting that on those panels nothing is collected either, so the IP Log freezes at its last value rather than merely going unpruned.
  • The intent is on your side. TestUpsertNodeClientIps_EmptyMergeDeletesRow (internal/web/service/inbound_node_ips_test.go:69-93) exists specifically so that "the merge yields nothing fresh, so the row must be removed (not left lingering)". Nothing in the tree pins the opposite behaviour, and the maintainer's comment you quoted from #4321 is accurate as quoted.
  • Your arithmetic checks out. I re-derived all three timestamps against 1787393116: 135281 s (37.6 h), 117261 s (32.6 h) and 132171 s (36.7 h), and the UTC conversions match.
  • "No supported way to clear it" is right for node_client_ips, not for the flat table. There is a per-client clear — POST /panel/api/clients/clearIps/:email (internal/web/controller/client.go:78), the same action the IP Log's clear button uses — but ClearClientIps only blanks inbound_client_ips.ips (inbound_client_ips.go:227-238) and leaves the attribution rows untouched. So it is a partial workaround for the display set only.

Verdict: Confirmed bug
Severity: Medium
Confidence: High
Root cause: CheckClientIpJob.processObserved (internal/web/job/check_client_ip_job.go:274-278, :307) only visits emails present in the current online-stats scan, and loadClientIpRows (:251) restricts the query to those emails, so the ipStaleAfterSeconds cutoff applied inside mergeClientIps (:519) is unreachable for any row whose client is offline. upsertNodeClientIps (internal/web/service/inbound_node_ips.go:69-84) has the same defect for node_client_ips despite already holding every row for the guid at :58-61. Triggered by any client that stops connecting; not a regression — the cutoff has been rewrite-scoped since eef2d311, and node_client_ips inherited the shape in 9385b6c6.
Already fixed: Still present on the default branch (1250fbb7); no change to these files since v3.6.0 other than d6472740 and 2a8c3bc0, neither of which touches the selection path.
Duplicate of: Related: #6285 — same "an unobserved row is never revisited" property, but a different root cause there (UpdateHeartbeat not re-keying or purging the previous guid). Related: #5423 raised the retention question as a feature request.
Evidence: check_client_ip_job.go:45 const ipStaleAfterSeconds = int64(30 * 60); :251 db.Where("client_email IN ?", batch).Find(&rows); :274-278, :307, :519; :62-64 fail2ban early return; inbound_node_ips.go:58-61 (all rows loaded) vs :69-84 (only incoming emails iterated) and :90-101 (delete unreachable for unobserved emails); :161-174 GetClientIpsByGuid filters before the wire, which is why the master's merge never prunes; inbound_client_ips.go:80,137 (cutoff applied per incoming row) with node_traffic_sync_job.go:436-454 (whole table shipped both ways); client_crud.go:844-849 deletes InboundClientIps and NodeClientTraffic but not NodeClientIp; inbound_node_ips_test.go:69-93 states the "not left lingering" intent.
Not determined: Whether the panel you queried is a master or a sub-node, and whether node IP sync was running on it. It changes only how much of inbound_client_ips was being swept indirectly; it does not affect the node_client_ips finding, which your log block is what actually demonstrates. select count(*) from nodes where enable = 1 and status = 'online'; on that box would settle it.

Suggested fix: A periodic full-table pass over inbound_client_ips and node_client_ips that applies the same cutoff regardless of observation and deletes rows that end up empty, as you proposed — both tables are one row per client per guid, so it is cheap next to the job's 10s cadence. It would need to run independently of the isFail2BanEnabled gate at check_client_ip_job.go:62-64, or panels with fail2ban disabled keep their frozen rows.

This reply was generated automatically; a maintainer may follow up.

<!-- gh-comment-id:5379831881 --> @github-actions[bot] commented on GitHub (Aug 22, 2026): Confirmed — this is a real bug and it is still present on the default branch (`1250fbb7`). Your reading of the mechanism is correct: `ipStaleAfterSeconds` is only ever applied while a row is being rewritten, and only rows whose email appears in the current online-stats scan are ever rewritten. ## Root cause `CheckClientIpJob.processObserved` derives its entire work list from the scan: ```go // internal/web/job/check_client_ip_job.go:274-278 emails := make([]string, 0, len(observed)) for email := range observed { emails = append(emails, email) } ``` `loadClientIpRows` then narrows the table to exactly those emails — `db.Where("client_email IN ?", batch).Find(&rows)` (`check_client_ip_job.go:251`) — and the loop at `:307` iterates `emails` only. The cutoff itself lives one layer further in, inside the rewrite: ```go // internal/web/job/check_client_ip_job.go:519 ipMap := mergeClientIps(oldIpsWithTime, newIpsWithTime, time.Now().Unix()-ipStaleAfterSeconds, observedAreLive) ``` So a row whose client stops connecting is never selected, never merged, and never pruned. `ipStaleAfterSeconds` (`check_client_ip_job.go:45`, `int64(30 * 60)`) is a cutoff applied on write, not a retention bound. `node_client_ips` has the same shape, and `upsertNodeClientIps` makes it especially visible: it already loads **every** row for the guid at `internal/web/service/inbound_node_ips.go:58-61`, then throws that away and iterates only the incoming emails — ```go // internal/web/service/inbound_node_ips.go:69-75, 84 emails := make([]string, 0, len(perEmail)) for email := range perEmail { ... } sort.Strings(emails) ... for _, email := range emails { ``` — so the `len(merged) == 0` delete at `:90-101` can only ever fire for a client that is still being observed. This is not a regression. The cutoff has been applied inside the merge since `eef2d311` ("Fix IP Limit continuous ban loop from stale DB entries (#4077)"), and `node_client_ips` inherited that shape when it was added in `9385b6c6`. ## Two things your report did not have **1. The flat table is partly swept on a master/node pair; `node_client_ips` is not.** `node_traffic_sync_job.go:436-454` ships the *whole* `inbound_client_ips` table in both directions (`FetchAllClientIps` → `MergeInboundClientIps`, then `PushAllClientIps` back), and `MergeInboundClientIps` applies the cutoff to every incoming row (`inbound_client_ips.go:80,137`). That gives `inbound_client_ips` an indirect full-table pass whenever IP sync runs. `node_client_ips` gets no equivalent, because `GetClientIpsByGuid` already drops stale entries and skips empty rows *before* they leave the node (`inbound_node_ips.go:161-174`) — the master therefore never sees a stale attribution row to prune. That is almost certainly why your evidence came out of `node_client_ips` rather than `inbound_client_ips`. On a standalone panel with no nodes, neither table is swept at all. **2. Deleted and renamed clients leave `node_client_ips` rows behind permanently.** Client deletion removes the flat row and the node traffic row but not the attribution row: ```go // internal/web/service/client_crud.go:844-849 if err := db.Where("client_email = ?", email).Delete(&model.InboundClientIps{}).Error; err != nil { ... } if err := db.Where("email = ?", email).Delete(&model.NodeClientTraffic{}).Error; err != nil { ... } ``` There is no `model.NodeClientIp` delete on that path, and `UpdateClientIPs` (`inbound_client_ips.go:154-163`) re-keys only the flat table on rename. Since nothing ever revisits an unobserved row, those become permanent orphans — that is row-count growth, not just frozen timestamps, and it strengthens the case beyond the retention argument. ## On your other points - **The `Run` early return is real.** `check_client_ip_job.go:62-64` returns before `processObserved` when `XUI_ENABLE_FAIL2BAN` is set to anything other than `true` (`isFail2BanEnabled`, `:472-475`; default is enabled, `.env.example:69`). Worth noting that on those panels nothing is *collected* either, so the IP Log freezes at its last value rather than merely going unpruned. - **The intent is on your side.** `TestUpsertNodeClientIps_EmptyMergeDeletesRow` (`internal/web/service/inbound_node_ips_test.go:69-93`) exists specifically so that "the merge yields nothing fresh, so the row must be removed (not left lingering)". Nothing in the tree pins the opposite behaviour, and the maintainer's comment you quoted from #4321 is accurate as quoted. - **Your arithmetic checks out.** I re-derived all three timestamps against `1787393116`: 135281 s (37.6 h), 117261 s (32.6 h) and 132171 s (36.7 h), and the UTC conversions match. - **"No supported way to clear it" is right for `node_client_ips`, not for the flat table.** There is a per-client clear — `POST /panel/api/clients/clearIps/:email` (`internal/web/controller/client.go:78`), the same action the IP Log's clear button uses — but `ClearClientIps` only blanks `inbound_client_ips.ips` (`inbound_client_ips.go:227-238`) and leaves the attribution rows untouched. So it is a partial workaround for the display set only. --- Verdict: Confirmed bug Severity: Medium Confidence: High Root cause: `CheckClientIpJob.processObserved` (`internal/web/job/check_client_ip_job.go:274-278`, `:307`) only visits emails present in the current online-stats scan, and `loadClientIpRows` (`:251`) restricts the query to those emails, so the `ipStaleAfterSeconds` cutoff applied inside `mergeClientIps` (`:519`) is unreachable for any row whose client is offline. `upsertNodeClientIps` (`internal/web/service/inbound_node_ips.go:69-84`) has the same defect for `node_client_ips` despite already holding every row for the guid at `:58-61`. Triggered by any client that stops connecting; not a regression — the cutoff has been rewrite-scoped since `eef2d311`, and `node_client_ips` inherited the shape in `9385b6c6`. Already fixed: Still present on the default branch (`1250fbb7`); no change to these files since `v3.6.0` other than `d6472740` and `2a8c3bc0`, neither of which touches the selection path. Duplicate of: Related: #6285 — same "an unobserved row is never revisited" property, but a different root cause there (`UpdateHeartbeat` not re-keying or purging the previous guid). Related: #5423 raised the retention question as a feature request. Evidence: `check_client_ip_job.go:45` `const ipStaleAfterSeconds = int64(30 * 60)`; `:251` `db.Where("client_email IN ?", batch).Find(&rows)`; `:274-278`, `:307`, `:519`; `:62-64` fail2ban early return; `inbound_node_ips.go:58-61` (all rows loaded) vs `:69-84` (only incoming emails iterated) and `:90-101` (delete unreachable for unobserved emails); `:161-174` `GetClientIpsByGuid` filters before the wire, which is why the master's merge never prunes; `inbound_client_ips.go:80,137` (cutoff applied per incoming row) with `node_traffic_sync_job.go:436-454` (whole table shipped both ways); `client_crud.go:844-849` deletes `InboundClientIps` and `NodeClientTraffic` but not `NodeClientIp`; `inbound_node_ips_test.go:69-93` states the "not left lingering" intent. Not determined: Whether the panel you queried is a master or a sub-node, and whether node IP sync was running on it. It changes only how much of `inbound_client_ips` was being swept indirectly; it does not affect the `node_client_ips` finding, which your log block is what actually demonstrates. `select count(*) from nodes where enable = 1 and status = 'online';` on that box would settle it. Suggested fix: A periodic full-table pass over `inbound_client_ips` and `node_client_ips` that applies the same cutoff regardless of observation and deletes rows that end up empty, as you proposed — both tables are one row per client per guid, so it is cheap next to the job's 10s cadence. It would need to run independently of the `isFail2BanEnabled` gate at `check_client_ip_job.go:62-64`, or panels with fail2ban disabled keep their frozen rows. *This reply was generated automatically; a maintainer may follow up.* <!-- claude-issue:analyst -->
Author
Owner

@rokokol commented on GitHub (Aug 22, 2026):

The log block came from a sub-node, not from the master, and node IP sync was running: select count(*) from nodes where enable = 1 and status = 'online'; returns 2 on the master. So the node_client_ips evidence falls exactly under the case described above — the node prunes before the wire, the master never sees a stale attribution row, and nothing sweeps the node's own copy.

The second finding reproduces here as well. On our master, 15 rows in node_client_ips belong to clients that no longer exist in clients, spread across all three live guids — so this is row-count growth in a live deployment, not just a theoretical path.

<!-- gh-comment-id:5379859570 --> @rokokol commented on GitHub (Aug 22, 2026): The log block came from a sub-node, not from the master, and node IP sync was running: `select count(*) from nodes where enable = 1 and status = 'online';` returns 2 on the master. So the `node_client_ips` evidence falls exactly under the case described above — the node prunes before the wire, the master never sees a stale attribution row, and nothing sweeps the node's own copy. The second finding reproduces here as well. On our master, 15 rows in `node_client_ips` belong to clients that no longer exist in `clients`, spread across all three live guids — so this is row-count growth in a live deployment, not just a theoretical path.
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#4269
No description provided.