Commit graph

6 commits

Author SHA1 Message Date
カン
0499e91ebe docs(database): clarify test coverage scope and experimental variable
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-29 15:41:30 +09:00
カン
b0cfc49c0d docs(database): scope the lock-prevention guarantee to intra-process access
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-29 15:41:30 +09:00
カン
ca49f5672a docs(database): fix proof claim — SetMaxOpenConns(1) is sufficient, not necessary
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-29 15:41:30 +09:00
カン
314f877d78 fix(database): address Copilot review comments
- Remove _synchronous=NORMAL from DSN to preserve SQLite's default
  durability guarantees; WAL mode alone is sufficient for the fix
- Add error handling for db.Exec and db.DB() in openTestDB helper
- Use context.WithTimeout(5s) in with_fix sub-test to prevent hang
  on connection-never-released regression

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-29 15:41:30 +09:00
カン
de4d40a25c style(database): fix gofmt comment indentation in concurrent_test.go
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-29 15:41:30 +09:00
カン
545f5a3672 fix(database): prevent "database is locked" with WAL mode and single connection pool
SQLite's default DELETE journal mode combined with an unbounded GORM
connection pool allows multiple goroutines (XrayTrafficJob every 10s,
PeriodicResetJob hourly/daily, WebSocket tickers) to each obtain their
own connection and race for the write lock. When a long-running
transaction holds the lock longer than go-sqlite3's busy_timeout the
competing writer fails with "database is locked" (issue #3739).

Two changes to InitDB:
- Enable WAL journal mode (_journal_mode=WAL, _synchronous=NORMAL):
  readers no longer block writers, reducing the write-lock window.
  Restores the fix from PR #2645 that was accidentally reverted in
  the "revert group management" commit (d18a1a37).
- SetMaxOpenConns(1): serialises all GORM access through a single
  connection at the Go pool level so SQLite write-lock contention
  cannot occur regardless of transaction duration.

Add database/concurrent_test.go with two test functions:
- TestConcurrentWrites: behavioural proof — reproduces the root cause
  (two connections + short busy_timeout → "database is locked"), then
  proves SetMaxOpenConns(1) eliminates it by serialising at pool level.
- TestInitDBConcurrencyConfig: verifies InitDB applies both settings,
  completing the chain of proof that the production code path is fixed.

Closes #3739

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-29 15:41:30 +09:00