Skip to content

Microsoft.Data.Sqlite: Don't retry SQLITE_BUSY_SNAPSHOT - #39587

Open
ajcvickers wants to merge 2 commits into
dotnet:mainfrom
ajcvickers:sqlite-fail-fast-busy-snapshot
Open

ajcvickers wants to merge 2 commits into
dotnet:mainfrom
ajcvickers:sqlite-fail-fast-busy-snapshot

Conversation

@ajcvickers

@ajcvickers ajcvickers commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Part of #29514

When a connection that has an open read transaction (an unfinished data reader, or a deferred transaction that has already read) tries to write after another connection has committed, SQLite returns SQLITE_BUSY with the extended code SQLITE_BUSY_SNAPSHOT (517). This can only happen in WAL mode, and the read snapshot stays stale until the reading transaction ends, so retrying can never succeed.

Microsoft.Data.Sqlite retries SQLITE_BUSY itself rather than using sqlite3_busy_timeout, so it kept retrying for the whole command timeout (30 seconds by default, and forever when the timeout is 0) before throwing exactly the same exception.

This PR makes the statement step loop in SqliteDataReader throw immediately for SQLITE_BUSY_SNAPSHOT. Plain SQLITE_BUSY, SQLITE_LOCKED and SQLITE_LOCKED_SHAREDCACHE are retried as before.

What is deliberately not changed

  • The prepare loop in SqliteCommand and RetryWhileBusy in SqliteConnectionInternal: preparing a statement doesn't start a write transaction and RetryWhileBusy only runs against fresh connections at open, so neither can see this error.
  • The commit-on-dispose retry in SqliteDataRecord: that statement already holds the write lock, so it can't fail with a stale snapshot. A comment explains the choice of overload.

Tests

New tests in SqliteTransactionTest cover BeginTransaction(), a write, an async write, a write with CommandTimeout = 0, and a write in a deferred transaction after the reader is disposed, each asserting it fails in well under the timeout. Two contrast tests show a write blocked by another writer in WAL mode is still retried (and still times out as SQLITE_BUSY if the other writer doesn't finish). I confirmed the async and deferred tests take the full 30 seconds without the fix.

Related

A separate PR, #39588, adds the extended error code and guidance to the exception message so the cause is easier to find. The two are independent.

🤖 Generated with Claude Code

@ajcvickers
ajcvickers requested a review from a team as a code owner November 10, 2026 10:47
Copilot AI balanced review requested due to automatic review settings November 10, 2026 10:47

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@ajcvickers

Copy link
Copy Markdown
Contributor Author

/cc @bricelam

When a connection holding a read transaction tries to write after another
connection has committed, SQLite reports SQLITE_BUSY with the extended code
SQLITE_BUSY_SNAPSHOT. The read snapshot stays stale until the reading
transaction ends, so the busy-retry loops could never succeed and only delayed
the error until the command timeout (30 seconds by default). Fail immediately
instead.

Part of dotnet#29514

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@ajcvickers
ajcvickers force-pushed the sqlite-fail-fast-busy-snapshot branch from c054fc8 to aca42e2 Compare November 14, 2026 11:40
Copilot AI balanced review requested due to automatic review settings November 14, 2026 11:40

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

- Move the tests that aren't about transactions to SqliteCommandTest and share
  the WAL TempDatabase helper through TestUtilities.
- Run the zero-timeout test with a cancellation token so that a regression
  fails instead of hanging the test run.
- Dispose the other connection's transaction and await the background rollback
  in a finally block.
- Don't let TempDatabase cleanup failures mask the real test failure.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 16, 2026 09:28

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants