[GH-ISSUE #4286] [3.0.1] addClientTraffic fails with "too many SQL variables" for >2978 clients #2924

Closed
opened 2026-10-31 20:28:55 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @jakobmelti on GitHub (May 12, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/4286

Describe the bug

Bug

At web/service/inbound.go:1921, tx.Save(dbClientTraffics) attempts to save
all client traffic records in a single SQL statement. With >2978 clients
(32766 SQLite max variables / 11 fields), SQLite rejects the batch and
NO traffic is recorded for ANY client.

Reproduce

  1. Add 3000+ clients to any inbound(s)
  2. Wait 5+ seconds
  3. Logs show: WARNING - X-UI: AddClientTraffic update data too many SQL variables
  4. Client traffic counters never increment

Impact

All deployments with >2978 clients have completely broken traffic accounting.

Fix

Use CreateInBatches or chunk the save loop:

batchSize := 1000
for i := 0; i < len(dbClientTraffics); i += batchSize {
    end := i + batchSize
    if end > len(dbClientTraffics) {
        end = len(dbClientTraffics)
    }
    if err := tx.Save(dbClientTraffics[i:end]).Error; err != nil {
        logger.Warning("AddClientTraffic update batch ", err)
    }
}

How to repeat the problem?

  1. Install or upgrade to 3x-ui 3.0.1 (any database mode — default SQLite)
  2. Create one or more VLESS/VMess/Trojan inbounds
  3. Add 3000+ clients total across the inbounds (the bug triggers above ~2978 clients)
  4. Wait 5-10 seconds for the XrayTrafficJob cron to fire
  5. Check the panel logs (Logs page in 3x-ui, or journalctl -u x-ui -f)
  6. Observe the warning appearing every 5 seconds:
    "WARNING - X-UI: AddClientTraffic update data too many SQL variables"
  7. Open the panel, expand any inbound, check client traffic counters
  8. Counters do NOT increment despite users actively transferring data through xray
  9. Verify xray IS counting traffic correctly:
  10. Confirm the SQL batch is failing:
    • Run: sqlite3 /etc/x-ui/x-ui.db "SELECT email, up, down FROM client_traffics LIMIT 5;"
    • up/down values stay at last successful save (often 0 or stale)

The bug triggers because web/service/inbound.go:1921 calls tx.Save(dbClientTraffics)
with ALL clients in a single SQL statement. With 11 DB-persisted fields per
ClientTraffic struct (id, inbound_id, enable, email, up, down, all_time,
expiry_time, total, reset, last_online), the placeholder count is:

client_count × 11 = total SQL placeholders

SQLite's SQLITE_MAX_VARIABLE_NUMBER default is 32766 (since SQLite 3.32, May 2020).

Maximum safe client count: 32766 ÷ 11 = 2978 clients

Beyond 2978 clients, EVERY traffic update fails with "too many SQL variables"
and traffic accounting is COMPLETELY broken for the entire deployment.

In my deployment (7399 clients), the failure rate is 100% — no traffic is
ever recorded. All counters in the panel show 0 or stale values.

Expected action

No response

Received action

No response

3x-ui Version

3.0.1

Xray-core Version

No response

Checklist

  • This bug report is written entirely in English.
  • This bug report is new and no one has reported it before me.
Originally created by @jakobmelti on GitHub (May 12, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/4286 ### Describe the bug ## Bug At web/service/inbound.go:1921, `tx.Save(dbClientTraffics)` attempts to save all client traffic records in a single SQL statement. With >2978 clients (32766 SQLite max variables / 11 fields), SQLite rejects the batch and NO traffic is recorded for ANY client. ## Reproduce 1. Add 3000+ clients to any inbound(s) 2. Wait 5+ seconds 3. Logs show: WARNING - X-UI: AddClientTraffic update data too many SQL variables 4. Client traffic counters never increment ## Impact All deployments with >2978 clients have completely broken traffic accounting. ## Fix Use CreateInBatches or chunk the save loop: ```go batchSize := 1000 for i := 0; i < len(dbClientTraffics); i += batchSize { end := i + batchSize if end > len(dbClientTraffics) { end = len(dbClientTraffics) } if err := tx.Save(dbClientTraffics[i:end]).Error; err != nil { logger.Warning("AddClientTraffic update batch ", err) } } ``` ### How to repeat the problem? 1. Install or upgrade to 3x-ui 3.0.1 (any database mode — default SQLite) 2. Create one or more VLESS/VMess/Trojan inbounds 3. Add 3000+ clients total across the inbounds (the bug triggers above ~2978 clients) 4. Wait 5-10 seconds for the XrayTrafficJob cron to fire 5. Check the panel logs (Logs page in 3x-ui, or `journalctl -u x-ui -f`) 6. Observe the warning appearing every 5 seconds: "WARNING - X-UI: AddClientTraffic update data too many SQL variables" 7. Open the panel, expand any inbound, check client traffic counters 8. Counters do NOT increment despite users actively transferring data through xray 9. Verify xray IS counting traffic correctly: - Run: curl -s http://127.0.0.1:62789/debug/vars (if metrics enabled) - Or check xray stats via gRPC API - You will see traffic IS being tracked by xray itself 10. Confirm the SQL batch is failing: - Run: sqlite3 /etc/x-ui/x-ui.db "SELECT email, up, down FROM client_traffics LIMIT 5;" - up/down values stay at last successful save (often 0 or stale) The bug triggers because web/service/inbound.go:1921 calls tx.Save(dbClientTraffics) with ALL clients in a single SQL statement. With 11 DB-persisted fields per ClientTraffic struct (id, inbound_id, enable, email, up, down, all_time, expiry_time, total, reset, last_online), the placeholder count is: client_count × 11 = total SQL placeholders SQLite's SQLITE_MAX_VARIABLE_NUMBER default is 32766 (since SQLite 3.32, May 2020). Maximum safe client count: 32766 ÷ 11 = 2978 clients Beyond 2978 clients, EVERY traffic update fails with "too many SQL variables" and traffic accounting is COMPLETELY broken for the entire deployment. In my deployment (7399 clients), the failure rate is 100% — no traffic is ever recorded. All counters in the panel show 0 or stale values. ### Expected action _No response_ ### Received action _No response_ ### 3x-ui Version 3.0.1 ### Xray-core Version _No response_ ### Checklist - [x] This bug report is written entirely in English. - [x] This bug report is new and no one has reported it before me.
bsakaguchi 2026-10-31 20:28:55 +07:00
  • closed this issue
  • added the
    bug
    label
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#2924
No description provided.