sqldb+lncfg: use SQLite-appropriate default for max connections
What changed, and why it matters
This commit fixes a configuration bug where LND's SQLite database layers used an unsuitable number of maximum database connections. One layer used the Postgres default (25), and another used 0 (unlimited). The patch introduces a shared SQLite-specific default of 2 connections, which matches SQLite's single-writer design and reduces resource contention. This is a reliability/resource-tuning fix rather than a direct security vulnerability, though excessive connections could contribute to denial-of-service conditions under load.
Treat as a routine reliability/resource-optimization patch. Deploy in normal maintenance. Monitor for any SQLite busy-timeout or concurrency regressions if operators had previously relied on higher connection counts.
Security signals we found
Resource exhaustion mitigation: unlimited/25 SQLite connections could worsen contention and memory use
Configuration consistency fix across two SQLite database layers
No input validation, cryptographic, or authorization changes
No memory-unsafe code or pointer arithmetic
No explicit security boundary crossed
Evidence from the diff
The change adds a MaxConns() helper to sqldb.SqliteConfig that returns the configured MaxConnections or defaults to 2. It updates both sqldb/sqlite.go (which previously used defaultMaxConns, a Postgres-oriented constant of 25) and lncfg/db.go (which previously passed 0/unlimited to sqlbase.Init when unconfigured) to use this shared default. The commit rationale explicitly states this reduces contention on SQLite’s busy_timeout and limits resource usage, especially on mobile.
Changed components
lncfg/db.gosqldb/config.gosqldb/sqlite.goSQLite database backend initializationInspect captured patch +21 / −3
diff --git a/lncfg/db.go b/lncfg/db.go
index 5bd4d2e..2dec1bd 100644
--- a/lncfg/db.go
+++ b/lncfg/db.go
@@ -207,7 +207,7 @@ func (db *DB) Init(ctx context.Context, dbPath string) error {
sqlbase.Init(db.Postgres.MaxConnections)
case db.Backend == SqliteBackend:
- sqlbase.Init(db.Sqlite.MaxConnections)
+ sqlbase.Init(db.Sqlite.MaxConns())
}
return nil
diff --git a/sqldb/config.go b/sqldb/config.go
index ebf386f..a5e8122 100644
--- a/sqldb/config.go
+++ b/sqldb/config.go
@@ -32,6 +32,12 @@ type SqliteConfig struct {
}
const (
+ // defaultSqliteMaxConns is the default number of maximum open
+ // connections for SQLite. SQLite only supports a single writer, so a
+ // low default reduces contention on the busy_timeout and limits
+ // resource usage, especially on mobile.
+ defaultSqliteMaxConns = 2
+
// defaultBusyTimeoutMs is the default busy_timeout value in
// milliseconds, used when no BusyTimeout is configured.
defaultBusyTimeoutMs = 5000
@@ -47,6 +53,18 @@ func (s *SqliteConfig) busyTimeoutMs() int64 {
return defaultBusyTimeoutMs
}
+// MaxConns returns the effective maximum number of open connections. If
+// MaxConnections is not set, it returns a default of 2. This low default is
+// chosen because SQLite only supports a single writer, which helps reduce
+// contention and resource usage.
+func (s *SqliteConfig) MaxConns() int {
+ if s.MaxConnections > 0 {
+ return s.MaxConnections
+ }
+
+ return defaultSqliteMaxConns
+}
+
// Validate checks that the SqliteConfig values are valid.
func (p *SqliteConfig) Validate() error {
if err := p.QueryConfig.Validate(true); err != nil {
diff --git a/sqldb/sqlite.go b/sqldb/sqlite.go
index ede2999..1ed2681 100644
--- a/sqldb/sqlite.go
+++ b/sqldb/sqlite.go
@@ -136,8 +136,8 @@ func NewSqliteStore(cfg *SqliteConfig, dbPath string) (*SqliteStore, error) {
err)
}
- db.SetMaxOpenConns(defaultMaxConns)
- db.SetMaxIdleConns(defaultMaxConns)
+ db.SetMaxOpenConns(cfg.MaxConns())
+ db.SetMaxIdleConns(cfg.MaxConns())
db.SetConnMaxLifetime(connIdleLifetime)
queries := sqlc.New(db)
Why this scored 23/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.