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

+51 -9
View File
@@ -33,6 +33,8 @@ namespace Void::Sqlite
out.fail_next_commit_rc = std::exchange(g_fault_config.fail_next_commit_rc, 0);
out.auto_rollback_on_faulted_commit =
std::exchange(g_fault_config.auto_rollback_on_faulted_commit, false);
out.commit_applied_before_error =
std::exchange(g_fault_config.commit_applied_before_error, false);
}
return out;
}
@@ -82,6 +84,16 @@ namespace Void::Sqlite
}
}
/// A WAL I/O error during COMMIT can be reported after the commit record
/// was written, so the wrapper cannot prove whether the transaction
/// committed. SQLite documents automatic rollback for other COMMIT
/// failures, and a rollback journal has crash recovery to resolve them.
[[nodiscard]] bool commit_outcome_unknown(
int rc, std::string_view journal_mode) noexcept
{
return (rc & 0xFF) == SQLITE_IOERR && journal_mode == "wal";
}
[[nodiscard]] Error sqlite_error_from_db(
sqlite3* db,
int primary_rc,
@@ -1400,7 +1412,9 @@ namespace Void::Sqlite
SqliteErrorCode::misuse,
m_state == TransactionState::RolledBack
? "Sqlite: commit() called on a transaction that was already rolled back"
: "Sqlite: commit() called on non-active transaction"));
: m_state == TransactionState::Unknown
? "Sqlite: commit() called after an unknown commit outcome; inspect the database"
: "Sqlite: commit() called on non-active transaction"));
}
if (!m_conn || m_conn->db == nullptr) {
m_state = TransactionState::RecoveryRequired;
@@ -1411,15 +1425,30 @@ namespace Void::Sqlite
const auto fault = consume_fault_commit();
if (fault.fail_next_commit_rc != 0) {
if (fault.auto_rollback_on_faulted_commit) {
bool applied = false;
if (fault.commit_applied_before_error) {
char* apply_err = nullptr;
applied = sqlite3_exec(
m_conn->db, "COMMIT", nullptr, nullptr, &apply_err) == SQLITE_OK;
if (apply_err) sqlite3_free(apply_err);
} else if (fault.auto_rollback_on_faulted_commit) {
(void)sqlite3_exec(m_conn->db, "ROLLBACK", nullptr, nullptr, nullptr);
}
if (sqlite3_get_autocommit(m_conn->db) == 0) {
if (applied) {
// The commit took effect even though the caller sees an error.
m_state = TransactionState::Unknown;
m_conn->active_transaction = false;
m_conn->active_transaction_id = 0;
} else if (sqlite3_get_autocommit(m_conn->db) == 0) {
m_state = TransactionState::Active;
m_conn->active_transaction = true;
m_conn->active_transaction_id = m_id;
} else {
m_state = TransactionState::RolledBack;
m_state = commit_outcome_unknown(
fault.fail_next_commit_rc, m_conn->caps.journal_mode)
? TransactionState::Unknown
: TransactionState::RolledBack;
m_conn->active_transaction = false;
m_conn->active_transaction_id = 0;
}
@@ -1450,15 +1479,23 @@ namespace Void::Sqlite
m_state = TransactionState::Active;
m_conn->active_transaction = true;
m_conn->active_transaction_id = m_id;
} else {
m_state = TransactionState::RolledBack;
m_conn->active_transaction = false;
m_conn->active_transaction_id = 0;
return Void::Result::fail(make_sqlite_error(
map_sqlite_rc(rc),
std::format("Sqlite: commit: {}", msg),
rc,
ext));
}
m_conn->active_transaction = false;
m_conn->active_transaction_id = 0;
const bool unknown = commit_outcome_unknown(rc, m_conn->caps.journal_mode);
m_state = unknown ? TransactionState::Unknown : TransactionState::RolledBack;
return Void::Result::fail(make_sqlite_error(
map_sqlite_rc(rc),
std::format("Sqlite: commit: {}", msg),
unknown
? std::format("Sqlite: commit outcome unknown after WAL I/O error; "
"inspect the database: {}", msg)
: std::format("Sqlite: commit: {}", msg),
rc,
ext));
}
@@ -1469,6 +1506,11 @@ namespace Void::Sqlite
if (m_state == TransactionState::RolledBack) {
return {};
}
if (m_state == TransactionState::Unknown) {
return Void::Result::fail(make_sqlite_error(
SqliteErrorCode::recovery_required,
"Sqlite: rollback after an unknown commit outcome; inspect the database"));
}
if (m_state != TransactionState::Active) {
return Void::Result::fail(make_sqlite_error(
SqliteErrorCode::misuse,