mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[PR #4120] [CLOSED] fix(database): prevent "database is locked" with WAL mode and single connection pool #5604
Labels
No labels
backlog
bug
clarification needed
documentation
duplicate
enhancement
github_actions
good first issue
invalid
javascript
New Feature
PR welcomed
pull-request
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
MHSanaei/3x-ui#5604
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/MHSanaei/3x-ui/pull/4120
Author: @kanghouchao
Created: 4/28/2026
Status: ❌ Closed
Base:
main← Head:fix/sqlite-database-is-locked📝 Commits (7)
545f5a3fix(database): prevent "database is locked" with WAL mode and single connection poolde4d40astyle(database): fix gofmt comment indentation in concurrent_test.go314f877fix(database): address Copilot review commentsca49f56docs(database): fix proof claim — SetMaxOpenConns(1) is sufficient, not necessaryb0cfc49docs(database): scope the lock-prevention guarantee to intra-process access0499e91docs(database): clarify test coverage scope and experimental variableee27768update too "// primary DB configuration variable under test."📊 Changes
2 files changed (+208 additions, -1 deletions)
View changed files
➕
database/concurrent_test.go(+201 -0)📝
database/db.go(+7 -1)📄 Description
Problem
Multiple goroutines — `XrayTrafficJob` (every 10 s), `PeriodicResetJob` (hourly/daily), WebSocket tickers — each obtain their own GORM connection and race for the SQLite write lock. When a long-running transaction (many clients, auto-renew logic) holds the lock longer than go-sqlite3's busy timeout, the competing writer fails with `database is locked`. This is the root cause of #3739.
The default DELETE journal mode means any concurrent write blocks all other writes. The unbounded GORM connection pool (`MaxOpenConns = 0`) means every goroutine can get its own connection and race.
Historical note: A correct fix was submitted in PR #2645 (`_journal_mode=WAL` + pragmas + `SkipDefaultTransaction`) but was accidentally reverted in commit `
d18a1a37` ("revert group management") along with an unrelated feature rollback. This PR restores and extends that fix.Fix
Two minimal changes to `InitDB` in `database/db.go`:
1. WAL journal mode
```go
sqlite.Open(dbPath + "?_journal_mode=WAL")
```
Readers no longer block writers and vice-versa, reducing the write-lock window.
2. Single connection pool
```go
sqlDB.SetMaxOpenConns(1)
```
Serialises all GORM database access through one connection at the Go `database/sql` pool level. A second writer waits for the pool connection to be free rather than racing at the SQLite level — eliminating write-lock contention entirely regardless of transaction duration.
Tests
`database/concurrent_test.go` adds two test functions:
Test run
```
$ go test -race -shuffle=on ./...
ok github.com/mhsanaei/3x-ui/v2/database 3.019s
ok github.com/mhsanaei/3x-ui/v2/database/model 1.478s
ok github.com/mhsanaei/3x-ui/v2/web/job 3.696s
ok github.com/mhsanaei/3x-ui/v2/web/service 3.148s
```
Closes #3739
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.