[GH-ISSUE #5121] [Bug]: Telegram bot and panel synchronization #3599

Closed
opened 2026-10-31 20:33:37 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @Sadegh-Kalami on GitHub (Jun 9, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/5121

Describe the bug

Whenever I modify client data on the Telegram bot, the panel doesn't update the modified setting, but in the background everything is working. It can cause race condition.

How to reproduce the problem

  1. Let client usage get depleted.
  2. In the Telegram bot, add more usage and modify the data.
  3. Check the panel, and the panel shows no update.

Expected behavior

No response

Actual behavior

No response

Relevant logs


Screenshots

No response

3x-ui version

3.2.7

Xray-core version

26.6.1

How did you install 3x-ui?

install.sh script

Operating system

Ubuntu 24.04

Which parts of the panel are affected?

Frontend (UI / panel pages), Backend (API endpoints, login, settings), Subscription (share links / Clash / JSON), Statistics / traffic counters, Telegram bot

Browser (only if it is a UI bug)

any browsers

Is the panel behind a reverse proxy or CDN?

No — direct access

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 @Sadegh-Kalami on GitHub (Jun 9, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/5121 ### Describe the bug Whenever I modify client data on the Telegram bot, the panel doesn't update the modified setting, but in the background everything is working. It can cause race condition. ### How to reproduce the problem 1. Let client usage get depleted. 2. In the Telegram bot, add more usage and modify the data. 3. Check the panel, and the panel shows no update. ### Expected behavior _No response_ ### Actual behavior _No response_ ### Relevant logs ```shell ``` ### Screenshots _No response_ ### 3x-ui version 3.2.7 ### Xray-core version 26.6.1 ### How did you install 3x-ui? install.sh script ### Operating system Ubuntu 24.04 ### Which parts of the panel are affected? Frontend (UI / panel pages), Backend (API endpoints, login, settings), Subscription (share links / Clash / JSON), Statistics / traffic counters, Telegram bot ### Browser (only if it is a UI bug) any browsers ### Is the panel behind a reverse proxy or CDN? No — direct access ### Before submitting - [x] I searched [existing issues](../issues?q=is%3Aissue) 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:33:37 +07:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@github-actions[bot] commented on GitHub (Jun 9, 2026):

This is a confirmed race condition between two concurrent code paths that both write to the client_traffics table without coordinating with each other.

Root cause

addClientTraffic (called by the traffic accounting cron job in web/job/xray_traffic_job.go → web/service/inbound.go:2241) does a full-row save on a snapshot it took at the start of the tick:

// inbound.go – addClientTraffic
err = tx.Model(xray.ClientTraffic{}).Where("email IN (?)", emails).Find(&dbClientTraffics).Error
// ... add traffic deltas to the snapshot ...
err = tx.Save(dbClientTraffics).Error   // ← overwrites ALL columns with the snapshot

Telegram bot client-config changes (limit_traffic_c → ResetClientTrafficLimitByEmail, reset_exp_c → ResetClientExpiryTimeByEmail, limit_ip_c → ResetClientIpLimitByEmail) call UpdateInboundClient → UpdateClientStat (web/service/inbound.go:2873), which writes specific columns (total, expiry_time, enable, reset) outside the submitTrafficWrite serializer.

If the traffic job reads the snapshot before the bot writes, and the job's tx.Save lands after the bot writes, it silently reverts the bot's change — setting total, expiry_time, and enable back to the values they had at the start of that traffic tick.

What submitTrafficWrite does and doesn't protect: the web/service/traffic_writer.go serializer covers all traffic-accumulation writes (AddTraffic, ResetClientTrafficByEmail, etc.) but not UpdateClientStat. That's the gap.

Note: the "reset traffic" button (reset_traffic_c) calls ResetClientTrafficByEmail which does go through submitTrafficWrite, so it is not affected. Only the change-limit, change-expiry, and change-IP-limit actions are vulnerable.

Why it appears to "work in the background"

After the bot writes, xray restarts (SetToNeedRestart) and picks up the new settings, so connections succeed for seconds or minutes — until the next traffic tick fires and tx.Save reverts the enable/total/expiry_time columns. The panel then reads the reverted DB row, which is why the change appears not to stick.

#5039 ("Telegram bot doesn't change expiration date") describes exactly the same race in the expiry path. The "message is not modified" Telegram warning in that report means searchClient read the reverted row immediately after the bot wrote, confirming the job had already overwritten it.

Workaround until fixed

  1. After using the bot to change a client's limit or expiry, wait 10–20 seconds for any in-flight traffic tick to finish, then apply the change again.
  2. Alternatively, use the panel UI directly for these changes — it goes through the same UpdateClientStat path but is unaffected in practice because the panel refresh is user-triggered and not racing a background saver on a tight loop.

Fix direction

addClientTraffic should use column-specific Updates (e.g., increment up/down, set last_online) rather than tx.Save on a stale full-row snapshot, or UpdateClientStat should be routed through submitTrafficWrite so it is serialized with the traffic job.

<!-- gh-comment-id:4658115853 --> @github-actions[bot] commented on GitHub (Jun 9, 2026): This is a confirmed race condition between two concurrent code paths that both write to the `client_traffics` table without coordinating with each other. ## Root cause **`addClientTraffic`** (called by the traffic accounting cron job in `web/job/xray_traffic_job.go` → `web/service/inbound.go:2241`) does a **full-row save** on a snapshot it took at the start of the tick: ```go // inbound.go – addClientTraffic err = tx.Model(xray.ClientTraffic{}).Where("email IN (?)", emails).Find(&dbClientTraffics).Error // ... add traffic deltas to the snapshot ... err = tx.Save(dbClientTraffics).Error // ← overwrites ALL columns with the snapshot ``` **Telegram bot client-config changes** (`limit_traffic_c` → `ResetClientTrafficLimitByEmail`, `reset_exp_c` → `ResetClientExpiryTimeByEmail`, `limit_ip_c` → `ResetClientIpLimitByEmail`) call `UpdateInboundClient` → `UpdateClientStat` (`web/service/inbound.go:2873`), which writes specific columns (`total`, `expiry_time`, `enable`, `reset`) outside the `submitTrafficWrite` serializer. If the traffic job reads the snapshot **before** the bot writes, and the job's `tx.Save` lands **after** the bot writes, it silently reverts the bot's change — setting `total`, `expiry_time`, and `enable` back to the values they had at the start of that traffic tick. **What `submitTrafficWrite` does and doesn't protect**: the `web/service/traffic_writer.go` serializer covers all traffic-accumulation writes (`AddTraffic`, `ResetClientTrafficByEmail`, etc.) but **not** `UpdateClientStat`. That's the gap. > Note: the "reset traffic" button (`reset_traffic_c`) calls `ResetClientTrafficByEmail` which **does** go through `submitTrafficWrite`, so it is not affected. Only the *change-limit*, *change-expiry*, and *change-IP-limit* actions are vulnerable. ## Why it appears to "work in the background" After the bot writes, xray restarts (`SetToNeedRestart`) and picks up the new settings, so connections succeed for seconds or minutes — until the next traffic tick fires and `tx.Save` reverts the `enable`/`total`/`expiry_time` columns. The panel then reads the reverted DB row, which is why the change appears not to stick. ## Related issue #5039 ("Telegram bot doesn't change expiration date") describes exactly the same race in the expiry path. The "message is not modified" Telegram warning in that report means `searchClient` read the reverted row immediately after the bot wrote, confirming the job had already overwritten it. ## Workaround until fixed 1. After using the bot to change a client's limit or expiry, **wait 10–20 seconds** for any in-flight traffic tick to finish, then apply the change again. 2. Alternatively, use the panel UI directly for these changes — it goes through the same `UpdateClientStat` path but is unaffected in practice because the panel refresh is user-triggered and not racing a background saver on a tight loop. ## Fix direction `addClientTraffic` should use column-specific `Updates` (e.g., increment `up`/`down`, set `last_online`) rather than `tx.Save` on a stale full-row snapshot, **or** `UpdateClientStat` should be routed through `submitTrafficWrite` so it is serialized with the traffic job.
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#3599
No description provided.