Sqlite: report unknown commit outcome after WAL I/O error (VL-SQLITE-02)

- Add TransactionState::Unknown: a failed COMMIT whose outcome cannot be
  proven (WAL I/O error after the commit record may be durable) is never
  reported as RolledBack.
- Keep the transaction Active when SQLite leaves it open after the error;
  reject commit/rollback on an Unknown transaction with a clear error.
- Extend the fault guard with commit_applied_before_error to model a lost
  commit acknowledgement, and cover Unknown, Active-retry and
  rollback-journal RolledBack paths.
This commit is contained in:
faumaray committed 2026-12-03 18:17:17 +03:00
1 parent 0002e74a7d
commit 7eaa96f9da
4 files changed
+165 -11

No files matched your search

@@ -637,3 +637,93 @@ Void::Testing::Test sqlite_transaction_reports_automatic_rollback()
Void::Testing::require(db.exec("PRAGMA max_page_count=1073741823").has_value());
}
}
Void::Testing::Test sqlite_failed_commit_outcome_is_unknown_not_rolled_back()
{
TempSqliteDir dir;
const auto db_path = dir.path / "unknown-commit.db";
auto db_r = open_database(db_path);
Void::Testing::require(db_r.has_value());
auto& db = *db_r;
Void::Testing::expect_eq(db.capabilities().journal_mode, std::string{"wal"});
Void::Testing::require(
db.exec("CREATE TABLE t(id INTEGER PRIMARY KEY, v TEXT)").has_value());
// A WAL I/O error can be reported after the commit record became durable.
// The wrapper must not claim the transaction was definitely rolled back.
{
auto tx = db.begin(BeginMode::Immediate);
Void::Testing::require(tx.has_value());
Void::Testing::require(
db.exec("INSERT INTO t VALUES(1, 'maybe committed')").has_value());
{
ScopedFaultGuard guard{FaultConfig{
.fail_next_commit_rc = SQLITE_IOERR,
.commit_applied_before_error = true}};
auto failed_commit = tx->commit();
Void::Testing::expect(!failed_commit.has_value());
Void::Testing::expect(failed_commit.error().code().value ==
static_cast<std::int32_t>(SqliteErrorCode::io));
}
Void::Testing::expect(tx->state() == TransactionState::Unknown);
Void::Testing::expect(!tx->is_active());
// The row is visible: reporting RolledBack would have been a lie.
auto count = db.prepare("SELECT COUNT(*) FROM t WHERE id = 1");
Void::Testing::require(count.has_value() && *count->step());
Void::Testing::expect_eq(*count->try_column_int(0), static_cast<std::int64_t>(1));
// The ambiguous transaction cannot be committed or rolled back again;
// the caller must inspect the database.
auto stale_commit = tx->commit();
Void::Testing::expect(!stale_commit.has_value());
Void::Testing::expect(stale_commit.error().code().value ==
static_cast<std::int32_t>(SqliteErrorCode::misuse));
auto stale_rollback = tx->rollback_checked();
Void::Testing::expect(!stale_rollback.has_value());
Void::Testing::expect(stale_rollback.error().code().value ==
static_cast<std::int32_t>(SqliteErrorCode::recovery_required));
}
// A WAL I/O error that leaves the transaction open keeps it Active so the
// caller can retry or roll back explicitly.
{
auto tx = db.begin(BeginMode::Immediate);
Void::Testing::require(tx.has_value());
Void::Testing::require(db.exec("INSERT INTO t VALUES(2, 'retry')").has_value());
{
ScopedFaultGuard guard{FaultConfig{.fail_next_commit_rc = SQLITE_IOERR}};
auto failed_commit = tx->commit();
Void::Testing::expect(!failed_commit.has_value());
Void::Testing::expect(tx->state() == TransactionState::Active);
Void::Testing::expect(sqlite3_get_autocommit(db.handle()) == 0);
}
Void::Testing::require(tx->rollback_checked().has_value());
}
// The same error in rollback-journal mode is a known rollback: SQLite has
// crash recovery for a hot journal, so the wrapper reports RolledBack.
{
OpenOptions rollback_opts;
rollback_opts.journal_mode = JournalMode::Rollback;
const auto rollback_path = dir.path / "rollback.db";
auto rdb_r = open_database(rollback_path, rollback_opts);
Void::Testing::require(rdb_r.has_value());
auto& rdb = *rdb_r;
Void::Testing::require(
rdb.exec("CREATE TABLE t(id INTEGER PRIMARY KEY)").has_value());
auto tx = rdb.begin(BeginMode::Immediate);
Void::Testing::require(tx.has_value());
Void::Testing::require(rdb.exec("INSERT INTO t VALUES(1)").has_value());
{
ScopedFaultGuard guard{FaultConfig{
.fail_next_commit_rc = SQLITE_IOERR,
.auto_rollback_on_faulted_commit = true}};
auto failed_commit = tx->commit();
Void::Testing::expect(!failed_commit.has_value());
}
Void::Testing::expect(tx->state() == TransactionState::RolledBack);
}
}