Skip to content

fix(storage): apply SQLite pragmas to every connection and bound the busy retry - #2289

Merged
mbrandenburger merged 2 commits into
hyperledger-labs:mainfrom
mbrandenburger:fix/2285-sqlite-pragmas-retry
Oct 27, 2026
Merged

mbrandenburger merged 2 commits into
hyperledger-labs:mainfrom
mbrandenburger:fix/2285-sqlite-pragmas-retry

Conversation

@mbrandenburger

Copy link
Copy Markdown
Member

Fixes #2285
openDB applied the default pragmas with one db.Exec, which database/sql runs on a single pooled connection. Every other read connection ran with busy_timeout=0 and foreign_keys=0, so a read there failed with SQLITE_BUSY at once. A test that holds four read connections showed the defaults on the first one only. The same Exec also overrode a busy_timeout set in the data source on that connection.

Changes

  • openDB adds the defaults to the data source as _pragma parameters, and modernc runs them on every new connection. A pragma the data source already sets, as _pragma=name(...) or as a driver shorthand such as _busy_timeout, is not added.
  • The cache_size=1000000000 default is dropped. Applied per connection, it would let page-cache memory grow with every read connection, and the read pool is unbounded by default. A user who wants it sets _pragma=cache_size(...).
  • retryWriteDB retries SQLITE_BUSY up to 3 times instead of recursing with no limit. It does not sleep, since each attempt already waits up to busy_timeout inside SQLite.
  • The KVS write DB is wrapped in NewRetryWriteDB like every other SQLite store.
  • docs/configuration.md documents the per-connection defaults, DSN precedence, that the pool settings apply to the read pool only, directory creation and the busy retry. It also fixes the example data source (&_txlock to ?_txlock).

Tests

  • TestOpenAppliesPragmasToEveryConnection checks the pragmas on four read connections for a plain path, a file: URI, a _pragma in the DSN and a shorthand in the DSN. TestWithDefaultPragmas covers the DSN rewriting.
  • TestRetryWriteDBBusy and TestKVSRetriesBusyWrite hold BEGIN EXCLUSIVE from another connection. An error mapper releases the lock on the first SQLITE_BUSY, so the tests count retries without depending on timing.
  • All new tests fail on main. The old retry loops until the test times out.

Compatibility

No exported API changes. Panurus uses DbProvider, Opts, Config, ErrorMapper and NewConfigProvider, which are unchanged, and wraps its own stores in a bounded BusyRetryWriteDB. Its read connections now get foreign_keys=ON, which was the intended default.

@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Hey @mbrandenburger 👋 thanks for the PR!
I'm your friendly PR Helper Bot 🤖 and I'll be riding shotgun on this one, keeping track of your PR's status to help you get it approved and merged.

This comment updates automatically as you push changes -- think of it as your PR's live scoreboard!
Here's the latest:


PR Checks

✅ DCO Sign-off -- All commits have valid sign-offs. Nice work!


❌ GPG Signature -- Heads up! The following commits don't have a verified GPG signature:

  • 6bcc8d4 fix(storage): apply SQLite pragmas to every connection and bound the busy retry
  • b40c71e fixup! fix(storage): apply SQLite pragmas to every connection and bound the busy retry

You'll need to sign your commits with GPG (e.g. git commit -S). See the Signing Guide for a step-by-step walkthrough.


✅ Merge Conflicts -- No merge conflicts detected. Smooth sailing!


✅ Issue Link -- Linked to #2285 (assigned to you).


⏳ All checks must pass before this PR can be reviewed. You've got this!

@github-actions github-actions Bot added the status: needs revision The pull request requires changes from the author before it can be reviewed or merged label Oct 27, 2026
@mbrandenburger mbrandenburger added status: needs review The pull request is ready for maintainer review and removed status: needs revision The pull request requires changes from the author before it can be reviewed or merged labels Oct 27, 2026
@mbrandenburger mbrandenburger added this to the 26Q4 milestone Oct 27, 2026

@adecaro adecaro left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mergify

mergify Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

This pull request does not currently match the merge queue conditions, so it cannot be queued from here. The box comes back if it matches again.

@github-actions github-actions Bot added status: needs revision The pull request requires changes from the author before it can be reviewed or merged and removed status: needs review The pull request is ready for maintainer review labels Oct 27, 2026
@mbrandenburger
mbrandenburger enabled auto-merge (squash) October 27, 2026 14:38
…busy retry

openDB applied the default pragmas with one db.Exec, which database/sql runs on a single pooled connection. journal_mode is stored in the file, but busy_timeout, synchronous, temp_store and foreign_keys are per connection, so every other read connection ran with busy_timeout=0 and failed with SQLITE_BUSY at once instead of waiting. The same Exec also overrode a busy_timeout set in the data source on that one connection.

openDB now adds the defaults to the data source as _pragma parameters, which modernc runs on every new connection. A pragma the data source already sets, as _pragma=name(...) or as a driver shorthand such as _busy_timeout, is not added, so the user's value applies. The cache_size=1000000000 default is dropped: as a per-connection setting it would let page-cache memory grow with every read connection, and the read pool is unbounded by default.

retryWriteDB retried SQLITE_BUSY by calling itself with no limit, so a lock held for good hung the caller. It now retries up to 3 times without a sleep, since each attempt already waits up to busy_timeout inside SQLite, and returns the last error. The KVS write DB is wrapped in it like every other SQLite store.

The tests hold four read connections and check the pragmas on each, including a data source that sets busy_timeout itself. The retry and KVS tests hold BEGIN EXCLUSIVE from another connection and use an error mapper that releases the lock on the first SQLITE_BUSY, so they do not depend on timing. docs/configuration.md documents the per-connection defaults, DSN precedence, that pool settings apply to the read pool only, directory creation and the busy retry.

Fixes hyperledger-labs#2285

Signed-off-by: Marcus Brandenburger <bur@zurich.ibm.com>
…nd the busy retry

Signed-off-by: Marcus Brandenburger <bur@zurich.ibm.com>
@mbrandenburger
mbrandenburger force-pushed the fix/2285-sqlite-pragmas-retry branch from edda6a8 to b40c71e Compare October 27, 2026 14:48
@mbrandenburger
mbrandenburger merged commit c5e3d02 into hyperledger-labs:main Oct 27, 2026
41 of 43 checks passed
@mbrandenburger
mbrandenburger deleted the fix/2285-sqlite-pragmas-retry branch October 27, 2026 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: needs revision The pull request requires changes from the author before it can be reviewed or merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(storage): apply SQLite pragmas to every connection and bound the busy retry

2 participants