[GH-ISSUE #6057] [Bug]: mtproto, node sync, and traffic jobs fail with "database is locked" #4189

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

Originally created by @qlampa on GitHub (Jul 20, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6057

Describe the bug

Getting error as of title when x-ui running for sometime. After reboot mtproto seems to start working again, not really sure what causes this. Used database is sqlite. By the way there's also other warning saying same for other services.

How to reproduce the problem

Expected behavior

No response

Actual behavior

No response

Relevant logs

Jul 20 12:08:25  /usr/local/x-ui/x-ui[470]: WARNING - mtproto job: get desired instances failed: database is locked
Jul 20 12:08:25  /usr/local/x-ui/x-ui[470]: WARNING - node heartbeat: load nodes failed: database is locked
Jul 20 12:08:25  /usr/local/x-ui/x-ui[470]: WARNING - node traffic sync: load nodes failed: database is locked
Jul 20 12:50:54  /usr/local/x-ui/x-ui[470]: WARNING - add inbound traffic failed: database is locked
Jul 20 13:06:34  /usr/local/x-ui/x-ui[470]: WARNING - mtproto job: get desired instances failed: database is locked
Jul 20 13:09:13  /usr/local/x-ui/x-ui[470]: WARNING - node traffic sync: load nodes failed: database is locked
Jul 20 13:09:13  /usr/local/x-ui/x-ui[470]: WARNING - node heartbeat: load nodes failed: database is locked
Jul 20 13:09:13  /usr/local/x-ui/x-ui[470]: WARNING - add inbound traffic failed: database is locked
Jul 20 13:09:28  /usr/local/x-ui/x-ui[470]: WARNING - node heartbeat: load nodes failed: database is locked
Jul 20 13:09:28  /usr/local/x-ui/x-ui[470]: WARNING - node traffic sync: load nodes failed: database is locked
Jul 20 13:13:00  /usr/local/x-ui/x-ui[470]: WARNING - client ip job err: database is locked
Jul 20 13:13:05  /usr/local/x-ui/x-ui[470]: WARNING - node traffic sync: load nodes failed: database is locked
Jul 20 13:13:05  /usr/local/x-ui/x-ui[470]: WARNING - node heartbeat: load nodes failed: database is locked

Screenshots

No response

3x-ui version

3.5.0

Xray-core version

26.7.11

How did you install 3x-ui?

install.sh script

Operating system

Debian 12

Which parts of the panel are affected?

No response

Browser (only if it is a UI bug)

No response

Is the panel behind a reverse proxy or CDN?

None

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 @qlampa on GitHub (Jul 20, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6057 ### Describe the bug Getting error as of title when x-ui running for sometime. After reboot mtproto seems to start working again, not really sure what causes this. Used database is sqlite. By the way there's also other warning saying same for other services. ### How to reproduce the problem - ### Expected behavior _No response_ ### Actual behavior _No response_ ### Relevant logs ```console Jul 20 12:08:25 /usr/local/x-ui/x-ui[470]: WARNING - mtproto job: get desired instances failed: database is locked Jul 20 12:08:25 /usr/local/x-ui/x-ui[470]: WARNING - node heartbeat: load nodes failed: database is locked Jul 20 12:08:25 /usr/local/x-ui/x-ui[470]: WARNING - node traffic sync: load nodes failed: database is locked Jul 20 12:50:54 /usr/local/x-ui/x-ui[470]: WARNING - add inbound traffic failed: database is locked Jul 20 13:06:34 /usr/local/x-ui/x-ui[470]: WARNING - mtproto job: get desired instances failed: database is locked Jul 20 13:09:13 /usr/local/x-ui/x-ui[470]: WARNING - node traffic sync: load nodes failed: database is locked Jul 20 13:09:13 /usr/local/x-ui/x-ui[470]: WARNING - node heartbeat: load nodes failed: database is locked Jul 20 13:09:13 /usr/local/x-ui/x-ui[470]: WARNING - add inbound traffic failed: database is locked Jul 20 13:09:28 /usr/local/x-ui/x-ui[470]: WARNING - node heartbeat: load nodes failed: database is locked Jul 20 13:09:28 /usr/local/x-ui/x-ui[470]: WARNING - node traffic sync: load nodes failed: database is locked Jul 20 13:13:00 /usr/local/x-ui/x-ui[470]: WARNING - client ip job err: database is locked Jul 20 13:13:05 /usr/local/x-ui/x-ui[470]: WARNING - node traffic sync: load nodes failed: database is locked Jul 20 13:13:05 /usr/local/x-ui/x-ui[470]: WARNING - node heartbeat: load nodes failed: database is locked ``` ### Screenshots _No response_ ### 3x-ui version 3.5.0 ### Xray-core version 26.7.11 ### How did you install 3x-ui? install.sh script ### Operating system Debian 12 ### Which parts of the panel are affected? _No response_ ### Browser (only if it is a UI bug) _No response_ ### Is the panel behind a reverse proxy or CDN? None ### 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:37:50 +07:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@github-actions[bot] commented on GitHub (Jul 20, 2026):

Title: SQLite lock contention across cron jobs, worsened by the switch to rollback-journal (DELETE) mode

Severity: Medium

Category: Reliability

Why this matters:
Every warning line in your log maps to a different cron job independently hitting SQLITE_BUSY ("database is locked") on the shared SQLite handle — it's not one misbehaving job:

  • mtproto job: get desired instances failed → internal/web/job/mtproto_job.go:28
  • node heartbeat: load nodes failed → internal/web/job/node_heartbeat_job.go:39
  • node traffic sync: load nodes failed → internal/web/job/node_traffic_sync_job.go:94
  • add inbound traffic failed → internal/web/job/xray_traffic_job.go:49
  • client ip job err → internal/web/job/check_client_ip_job.go:460

Each job simply returns on error and waits for its next tick, so nothing is corrupted, but traffic accounting, node up/down detection, mtg sidecar reconciliation, and IP-limit enforcement all silently skip that cycle. This is also a long-running, recurring class of report (#3739 and older ones going back to #179/#522/#728/#1777), so it's worth treating as a genuine reliability gap rather than a one-off.

Two things compound to cause it, both confirmed in the current source (same as what ships in v3.5.0, the version you reported):

  1. No jitter between synchronized cron ticks. internal/web/web.go:289-293 schedules cadenceXrayTraffic, cadenceNodeHeartbeat, and cadenceNodeTraffic all at @every 5s, and cadenceMtproto / cadenceClientIPScan both at @every 10s — all registered back-to-back at startup with no stagger, so they tend to land on the same tick. The comment directly above that block already flags this: "add per-tick jitter to de-synchronize fleet load" is listed as a not-yet-implemented follow-up (web.go:280-285).
  2. Rollback-journal mode instead of WAL. Commit e079490144 ("chore(db): use DELETE journal mode so sqlite stays a single file", 2026-06-19) changed the DSN from _journal_mode=WAL&..._synchronous=NORMAL to _journal_mode=DELETE&..._synchronous=FULL (internal/database/db.go:1790, pragmas at 1800-1807), intentionally trading concurrency for a single x-ui.db file at rest with no -wal/-shm sidecars. In DELETE mode a writer's commit needs an exclusive lock that blocks concurrent readers (and vice versa); in WAL mode readers and writers don't block each other. busy_timeout is correctly set to 10s in both the DSN and a PRAGMA (db.go:1790, 1802) and does apply per pooled connection, so this isn't a missing-timeout bug — contention is genuinely exceeding 10s on installs like yours. For example, InboundService.AddTraffic (internal/web/service/inbound_traffic.go:24-36) opens one write transaction covering inbound traffic, client traffic, auto-renew and auto-disable every 5s (xray_traffic_job.go:47), which can hold the write lock long enough to block the plain SELECTs NodeService.GetAll() (internal/web/service/node.go:103-106) runs on the same 5s cadence.

Recommendation:

  • Implement the jitter already flagged as a follow-up at web.go:280-285 so the 5s/10s jobs stop landing on the same instant.
  • Re-examine the DELETE+FULL trade-off from e079490144. If the single-file-at-rest property mainly matters for backup/export, keeping WAL for normal operation and checkpointing (database.Checkpoint() is already wired into the export/import paths at internal/web/service/server.go:1307 and :1425) around that specific operation would preserve both properties without paying the concurrency cost on every tick.
  • internal/web/service/traffic_writer.go already serializes the traffic-write path through one goroutine (submitTrafficWrite), so writes have partial protection; the node-heartbeat/traffic-sync/mtproto reads in this report aren't part of that queue and can still be blocked by whatever write is in flight under DELETE mode.

As a config-only mitigation to test in the meantime: set XUI_DB_SYNCHRONOUS=NORMAL in /etc/default/x-ui and systemctl restart x-ui (read at internal/database/db.go:1854-1865; default is FULL, which fsyncs on every commit). This shortens how long each write holds the lock, which should reduce — not eliminate — how often the 10s busy_timeout is exceeded. It's an existing, supported env var, but it does slightly weaken crash durability versus FULL: SQLite may lose the most recent commit (not corrupt the file) if the OS crashes or power is lost immediately after a write.

Example:

// internal/web/web.go:289-293
cadenceXrayTraffic   = "@every 5s"
cadenceMtproto       = "@every 10s"
cadenceClientIPScan  = "@every 10s"
cadenceNodeHeartbeat = "@every 5s"
cadenceNodeTraffic   = "@every 5s"

Confidence: High that the mechanism above is a real, currently-present contributor to these warnings — grounded directly in the current source and the commit that introduced the DELETE-mode switch. Medium on whether it's the sole cause on this specific server, since lock hold time also scales with disk I/O speed and the number of inbounds/clients/nodes, which aren't in the report.

If you can share roughly how many inbounds/clients/nodes this panel manages and whether the disk is local SSD or network-backed storage, that would help gauge how much headroom the jitter/WAL changes would need to provide.

@MHSanaei for a decision on the journal-mode trade-off and/or cadence jitter.

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

<!-- gh-comment-id:5022629899 --> @github-actions[bot] commented on GitHub (Jul 20, 2026): **Title:** SQLite lock contention across cron jobs, worsened by the switch to rollback-journal (`DELETE`) mode **Severity:** Medium **Category:** Reliability **Why this matters:** Every warning line in your log maps to a different cron job independently hitting `SQLITE_BUSY` ("database is locked") on the shared SQLite handle — it's not one misbehaving job: - `mtproto job: get desired instances failed` → `internal/web/job/mtproto_job.go:28` - `node heartbeat: load nodes failed` → `internal/web/job/node_heartbeat_job.go:39` - `node traffic sync: load nodes failed` → `internal/web/job/node_traffic_sync_job.go:94` - `add inbound traffic failed` → `internal/web/job/xray_traffic_job.go:49` - `client ip job err` → `internal/web/job/check_client_ip_job.go:460` Each job simply `return`s on error and waits for its next tick, so nothing is corrupted, but traffic accounting, node up/down detection, mtg sidecar reconciliation, and IP-limit enforcement all silently skip that cycle. This is also a long-running, recurring class of report (#3739 and older ones going back to #179/#522/#728/#1777), so it's worth treating as a genuine reliability gap rather than a one-off. Two things compound to cause it, both confirmed in the current source (same as what ships in v3.5.0, the version you reported): 1. **No jitter between synchronized cron ticks.** `internal/web/web.go:289-293` schedules `cadenceXrayTraffic`, `cadenceNodeHeartbeat`, and `cadenceNodeTraffic` all at `@every 5s`, and `cadenceMtproto` / `cadenceClientIPScan` both at `@every 10s` — all registered back-to-back at startup with no stagger, so they tend to land on the same tick. The comment directly above that block already flags this: "add per-tick jitter to de-synchronize fleet load" is listed as a not-yet-implemented follow-up (`web.go:280-285`). 2. **Rollback-journal mode instead of WAL.** Commit `e079490144` ("chore(db): use DELETE journal mode so sqlite stays a single file", 2026-06-19) changed the DSN from `_journal_mode=WAL&..._synchronous=NORMAL` to `_journal_mode=DELETE&..._synchronous=FULL` (`internal/database/db.go:1790`, pragmas at 1800-1807), intentionally trading concurrency for a single `x-ui.db` file at rest with no `-wal`/`-shm` sidecars. In `DELETE` mode a writer's commit needs an exclusive lock that blocks concurrent readers (and vice versa); in WAL mode readers and writers don't block each other. `busy_timeout` is correctly set to 10s in both the DSN and a PRAGMA (`db.go:1790`, `1802`) and does apply per pooled connection, so this isn't a missing-timeout bug — contention is genuinely exceeding 10s on installs like yours. For example, `InboundService.AddTraffic` (`internal/web/service/inbound_traffic.go:24-36`) opens one write transaction covering inbound traffic, client traffic, auto-renew and auto-disable every 5s (`xray_traffic_job.go:47`), which can hold the write lock long enough to block the plain `SELECT`s `NodeService.GetAll()` (`internal/web/service/node.go:103-106`) runs on the same 5s cadence. **Recommendation:** - Implement the jitter already flagged as a follow-up at `web.go:280-285` so the 5s/10s jobs stop landing on the same instant. - Re-examine the `DELETE`+`FULL` trade-off from `e079490144`. If the single-file-at-rest property mainly matters for backup/export, keeping WAL for normal operation and checkpointing (`database.Checkpoint()` is already wired into the export/import paths at `internal/web/service/server.go:1307` and `:1425`) around that specific operation would preserve both properties without paying the concurrency cost on every tick. - `internal/web/service/traffic_writer.go` already serializes the traffic-write path through one goroutine (`submitTrafficWrite`), so writes have partial protection; the node-heartbeat/traffic-sync/mtproto reads in this report aren't part of that queue and can still be blocked by whatever write is in flight under `DELETE` mode. As a config-only mitigation to test in the meantime: set `XUI_DB_SYNCHRONOUS=NORMAL` in `/etc/default/x-ui` and `systemctl restart x-ui` (read at `internal/database/db.go:1854-1865`; default is `FULL`, which fsyncs on every commit). This shortens how long each write holds the lock, which should reduce — not eliminate — how often the 10s `busy_timeout` is exceeded. It's an existing, supported env var, but it does slightly weaken crash durability versus `FULL`: SQLite may lose the most recent commit (not corrupt the file) if the OS crashes or power is lost immediately after a write. **Example:** ```go // internal/web/web.go:289-293 cadenceXrayTraffic = "@every 5s" cadenceMtproto = "@every 10s" cadenceClientIPScan = "@every 10s" cadenceNodeHeartbeat = "@every 5s" cadenceNodeTraffic = "@every 5s" ``` **Confidence:** High that the mechanism above is a real, currently-present contributor to these warnings — grounded directly in the current source and the commit that introduced the `DELETE`-mode switch. Medium on whether it's the sole cause on this specific server, since lock hold time also scales with disk I/O speed and the number of inbounds/clients/nodes, which aren't in the report. If you can share roughly how many inbounds/clients/nodes this panel manages and whether the disk is local SSD or network-backed storage, that would help gauge how much headroom the jitter/WAL changes would need to provide. @MHSanaei for a decision on the journal-mode trade-off and/or cadence jitter. *This reply was generated automatically; a maintainer may follow up.*
Author
Owner

@MHSanaei commented on GitHub (Jul 23, 2026):

Fixed in #6083. SQLite now defaults to WAL, so background jobs no longer serialize into 'database is locked'. Checkpoint() truncates the WAL so backups stay single-file; XUI_DB_JOURNAL_MODE=DELETE restores the old mode.

<!-- gh-comment-id:5059093458 --> @MHSanaei commented on GitHub (Jul 23, 2026): Fixed in #6083. SQLite now defaults to WAL, so background jobs no longer serialize into 'database is locked'. Checkpoint() truncates the WAL so backups stay single-file; XUI_DB_JOURNAL_MODE=DELETE restores the old mode.
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#4189
No description provided.